John Davidson

php - Woocommerce discount by user role

0 comments
Message:


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 €


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);
}


My Answer:
It looks like the issue you are facing is related to the calculation of prices with discounts and taxes in WooCommerce. The discrepancy in prices between the product archive page and the cart/checkout page could be due to how taxes are applied to the discounted prices.

One thing you can try is to make sure that the prices you are modifying in your custom_price function are excluding taxes. WooCommerce has functions to get prices excluding taxes, such as wc_get_price_excluding_tax(). You can use this function to get the price without taxes before applying your discount percentage.

Here is an updated version of your custom_price function using wc_get_price_excluding_tax():

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

$price_excluding_tax = wc_get_price_excluding_tax($product);

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


By using wc_get_price_excluding_tax(), you ensure that the discount percentage is applied to the price before taxes are calculated, which should help with the discrepancy in prices between different pages in WooCommerce.

I hope this helps resolve the issue you are facing with the pricing calculation in WooCommerce. Let me know if you have any other questions or need further assistance.

Rate this post

4 of 5 based on 9923 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us