1

On our website we've got the function, which is displayed bellow, to change prices depending on user role. The problem is that if a customer logs in and has the premium role the prices will stay the same as if the customer isn't in that role. Is there a way to recalculate the cart prices?

add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);
add_filter('woocommerce_product_variation_get_price', 'return_custom_price', $product, 2);

function return_custom_price($price, $product) {    
    if (! current_user_can('premium') || ! is_user_logged_in()) {
        global $post, $woocommerce;
        return $new_price = $price * 1.25;   
    }
    return $price;
} 
1
  • Hook available for that name is something before calculating total Jun 28 '19 at 18:26
2

Maybe you need to add :

wc_delete_product_transients($post->ID);

I did something like this in opther project and thats worked:

 if (is_user_logged_in()) {
add_filter('woocommerce_product_variation_get_regular_price', 'my_custom_price', 10, 2);
add_filter('woocommerce_product_variation_get_price','my_custom_price', 10, 2); } function my_custom_price( $price ) {
global $post;

// Delete product cached price  (if needed)
wc_delete_product_transients($post->ID);

// Return the new price

return $price = ($price * 1.1) ;}

Not the answer you're looking for? Browse other questions tagged or ask your own question.