Woocommerce discount by user role

EDIT: As you see I'm new here. So sorry for giving you a headache This is the problem at the moment: The code is working now! But maybe it has something to do with taxes when the product price on shop page differs from cart and checkout page.

Just to be clear I've inserted the price with no VAT included and on shop page the VAT is not included. But on cart page the VAT is calculated.

Summary:

Shop page – Quick cart open

I'm currently working on a very complicated project with Wordpress + Woocommerce and I'm not so familiar with PHP and Wordpress/Woocommerce hooks. I'm trying to apply a percentage discount to products with category X. I'm running into issues like

A non-numeric value encountered

on dashboard and the price differs from product archive page on cart/checkout page.

Here's my code on functions.php:

add_filter('woocommerce_product_variation_get_price', 'custom_price', 10, 2);
add_filter('woocommerce_product_get_price', 'custom_price', 10, 2);
add_filter('woocommerce_get_price', 'custom_price', 10, 2);

function custom_price($price, $product)
{
  if (!is_user_logged_in()) return $price;

  if (has_role('hintaluokka-5')) {
    $price = $price * 0.95;
  } elseif (has_role('hintaluokka-12')) {
    $price = $price * 0.88;
  } elseif (has_role('hintaluokka-15')) {
    $price = $price * 0.85;
  } elseif (has_role('hintaluokka-22')) {
    if (has_term('kastikkeet', 'product_cat', $product->ID)) {
      $price = $price * 0.78;
    } else {
      $price = $price * 0.9;
    }
  }
  return $price;
}

function has_role($role = '', $user_id = null)
{
  if (is_numeric($user_id))
    $user = get_user_by('id', $user_id);
  else
    $user = wp_get_current_user();

  if (empty($user))
    return false;

  return in_array($role, (array) $user->roles);
}

Upvotes: 0

Views: 1038

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 77045

Look at this line:

$price = $price * 0.95;

What happens if $price is "1$"? In that case $price cannot be converted into a number in order to perform the calculate the result of the quoted formula. So, in order to solve your problem, you need to identify the exact line where the error occurs (from server error logs), find out what the variable/parameter causing the error is and convert it into a numeric value.

Upvotes: 0

Mr. Jo
Mr. Jo

Reputation: 5281

Try this code:

add_filter( 'woocommerce_product_variation_get_price', 'custom_price', 10, 2 );
add_filter( 'woocommerce_product_get_price', 'custom_price', 10, 2 );
function custom_price( $price, $product ) {
    if ( ! is_user_logged_in() ) {
        return $price;
    }

    if ( has_role( 'hintaluokka-5' ) ) {
        $price *= 0.95;
    } elseif ( has_role( 'hintaluokka-12' ) ) {
        $price *= 0.88;
    } elseif ( has_role( 'hintaluokka-15' ) ) {
        $price *= 0.85;
    } elseif ( has_role( 'hintaluokka-22' ) ) {
        if ( has_term( 'kastikkeet', 'product_cat', $product->ID ) ) {
            $price *= 0.78;
        } else {
            $price *= 0.9;
        }
    }

    return $price;
}

function has_role( $role = '', $user_id = null ) {
    if ( is_numeric( $user_id ) ) {
        $user = get_user_by( 'id', $user_id );
    } else {
        $user = wp_get_current_user();
    }

    if ( empty( $user ) ) {
        return false;
    }

    return in_array( $role, $user->roles, true );
}

I don't see a huge problem here. There is just one hook which is no longer used (deprecated).

Post the error message in case this don't helps.

Upvotes: 2

Related Questions