I've been trying to implement a discount in my store with the following code:
add_action('woocommerce_before_calculate_totals', 'set_discount', 10 );
function set_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Loop Through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// 50% items discount
$cart_item['data']->set_price( $cart_item['data']->get_price() / 2 );
}
}
- The code works on the cart page, the discount is applied as it should
- The code works on the checkout page for a second or two, BUT when then the loading spinner has finalized the discount disappears and the original price is shown
It seems the set price is overwritten in the checkout by some standard AJAX request which reuses the original price, or something. I have tried using add_fee() with the same result, I have also tried deactivating all plugins (except woocommerce, of course) and I have tried switching to another theme - nothing works!
Using Wordpress 5.0.3, Woocommerce 3.5.4, Child theme of Storefront 2.4.2
UPDATE: Added screenshots.
1) This is what should be shown on checkout, and is being shown for around 1-2 seconds:
2) This is what is shown once the loading spinner is finalized - the original prices: