I have two coupons, one $10 another $5. I want do next, if price be less $50 and customer enter discount coupon $10 replace this coupon by coupon $5.
Example:
The customer added an item in cart with total price $40 and he applied the coupon $10-Off. Now I need replace current coupon with $10-Off to my another coupon $5-Off and continue checkout.
What is the best and simple way I can use? I try change discount price like in code below and this working, but in other places WC show me old coupon and calculate all data from old coupon (in order page etc) so I really need REPLACE one coupon to another ( not calculate their price ) when this coupon added. It like customer enter $5-Off coupon directly to discount field
// Change price for discount
if( ! function_exists('custom_discount') ){
function custom_discount( $price, $values, $instance ) {
//$price represents the current product price without discount
//$values represents the product object
//$instance represent the cart object
//Get subtotal
$subtotal = WC()->cart->get_subtotal();
$coupon = WC()->cart->get_discount_total();
//if price < 50 and we have active coupon then we need decrease price to $5
if( $subtotal < 50 && ! empty( WC()->cart->get_applied_coupons() ) ){
$price = $subtotal - 5; //Wrong way ...
}
return $price;
}
add_filter( 'woocommerce_get_discounted_price', 'custom_discount', 10, 3);
}