I have implemented this custom code to programmatically discount prices for logged-in users. The discount correctly applies to variation prices, after I choose a variation.
I slightly changed the code to not take into account already discounted products, so my code looks like this
Edit: I also added condition to exclude gift card products from the discount.
add_filter( 'woocommerce_product_get_price', 'custom_discount_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'custom_discount_price', 10, 2 );
function custom_discount_price( $price, $product ) {
// For logged in users
if ( is_user_logged_in() ) {
$discount_rate = 0.95; // 5% of discount
// Product is on sale or is a gift card
if ( $product->is_on_sale() || $product->get_id() == 5770 || $product->get_id() == 5766 || $product->get_id() == 5764 ) {
return $price;
}
// Product is not on sale
else {
// Returns the custom discounted price
return $price * $discount_rate;
}
}
return $price;
}
}
But on the category page and even on the single variable product page there is this range telling the customers what the product's min variation price to max variation price is and this price range doesn't show discounted prices, but the original ones.
So for example:
I programatically discounted all the products by 90%.
A product has three variations -> X, Y, Z.
For variation X I set a price $10.
For variation Y I set a price $20.
For variation Z I set a price $30.
On category page I can see the product with it's image and the title -> Product: $10 - 30$.
But it should actually display prices $1 - $3, because I programatically discounted all the prices.
I'm looking everywhere and trying anythig, but to no results. Is there a way to achieve this? Thank you.
Edit: I changed the discount rate in the code snippet to 0.95 to match my provided answer.