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:
- Product price is: 10 €
- With 22% discount it’s 7.80 €
- With 24% VAT it’s: ~ 9.67 €
- But on cart and checkout page it says total: 11.16 €
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);
}
2
Answers
Try this code:
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.
Look at this line:
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.