1

I am developing a Woocommerce shop and want to offer all members (users logged in) 20% off their Total.

I have created a coupon called "twentypercent_off" and I am programmatically applying this using a custom function and a hook in my functions.php. This works well.

In WC settings I have checked "Calculate coupon discounts sequentially" and this works almost as I would expect. However what I would like is that my "twentypercent_off" is always applied last in cases where there are multiple coupons.

No matter what I have attempted to do (loop through all coupons on current order, remove all coupons, add them back in the order I desire) I find that Woo-commerce seems to always apply them in the order of monetary savings.

For example if the customer orders $100 worth of products and they have a coupon for $25 and also qualified to receive 20% off total.

I would like the total to calculate as (100 -25) * 0.8 = $60

but WC is calculating as such (100 * 0.8) - 25 = $55

I tried the following but it made no difference:

if ( is_user_logged_in() ) {
        $Coupon_code = 'twentypercent_off'; 
        $Current_Coupons = WC()->cart->get_coupons();
        $Non_Prog_Coupons=array();
        // loop through all current coupons
        foreach( $Current_Coupons as $code => $coupon ){
            if($code==$coupon_code){
                WC()->cart->remove_coupon( $code );
            } else {
                WC()->cart->remove_coupon( $code );
                $Non_Prog_Coupons[]=$code;  // array of non-programmatically added coupons
            }
        }
        // loop through non-programmatically added coupons
        foreach( $Non_Prog_Coupons as $x => $code ){
            WC()->cart->add_discount( $code );  // add them again 
        }
        // add programmatically added coupon last !
        WC()->cart->add_discount($Coupon_code);
    }
    return;

The only possible solution I have come across is to apply the 20% discount as a negative fee, which is not suitable for our purposes and would be an absolute last resort

Any ideas on how to achieve this would be greatly appreciated

radiation2
  • 11
  • 2

0 Answers0