In Woocommerce I have a variable products with many variations and every variation has its own REGULAR price and SALE price.
I would like when user:
- is logged in, the sale price will be the active price
- is not logged in, the regular price will be the active price.
I have added this hooked function in my child theme function.php file:
add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
wc_delete_product_transients($variation->get_id());
$variation_id = $variation->get_id();
$variable_product1= new WC_Product_Variation($variation_id);
if(is_user_logged_in()){
$regular_price = $variable_product1 ->regular_price;
$data['price_html'] = woocommerce_price($regular_price);
return $data;
} else {
$sale_price = $variable_product1 ->sale_price;
$data['price_html'] = woocommerce_price($sale_price);
return $data;
}
}
The displayed price works… But when I add to cart any product it remains only with the sale price in in cart…
Any help is appreciated
The code bellow will:
The code:
Code goes in function.php file of your active child theme (or active theme). Tested and works.