I was just wondering if anyone has a solution for this problem I am currently having. I am actually trying to print a customise price on the shopping cart for my users who are accessing my online store. Below are the conditions which I plan to set.
user who are not logged in -> No Discount, not suppose to see the customise pricing display on my shopping cart page.
user who are logged in as "customer" user role -> No Discount, not suppose to see the customise pricing display on my shopping cart page.
user who are logged in as "paid_customer" user role -> Discount applied, suppose to see the customise pricing display on my shopping cart page.
Currently only users which are not logged in and "paid_customer" user role have their pricing display working correctly but not for "customer" role. Not sure if i am identifying the customer role correctly over here.
Below is the hook which I am using for this condition:
add_filter( 'woocommerce_cart_item_price', function ( $price, $values, $cart_item_key ){
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$user = wp_get_current_user();
if (!is_user_logged_in() || (in_array( 'customer', (array) $user->roles ))){
return $price;
}else{
foreach($items as $item => $values) {
echo "Discounted Price : " . get_post_meta($values['product_id'] , ('_sale_price', true);
return $price;
}
}
}, 10, 3);
Edit:
My pricing discounts are already managed by Advanced dynamic pricing for WooCommerce plugin, based on my custom user roles, so I don’t need to worry about the price output anymore.
See my code answer below, based on @LoicTheAztec accepted answer.
3
Answers
This is what I did to accomplish what I wanted (thanks to @LoicTheAztec answer):
My pricing discounts are already managed by Advanced dynamic pricing for WooCommerce plugin, based on my custom user roles, so I don't need to worry about the price output anymore.
Use this hook instead "woocommerce_before_calculate_totals"
In that way you no need to handle price during checkout and place order process.
Your code has some mistakes and can be simplified:
It should work.
An edit to your answer code that is incorrect as the data in filter hooks needs always to be returned, but not echoed:
This is the correct and working way.