Repository Forums Support WooCommerce Extended Coupon Features PRO Minimum subtotal agreed products does not work Reply To: Minimum subtotal agreed products does not work

#2495
Soft79
Keymaster

If you use the following snippet, the coupon will only apply if you have 3 or more items in the cart.

1. Create a € 5 discount coupon
2. On the Usage Restrictions-tab select the Product to which it must apply
3. Append the following snippet to your child themes’ functions.php (replace your_coupon_code by the actual coupon code):


add_action( 'wjecf_assert_coupon_is_valid', 
    function( $coupon, $wc_discounts ) {
        if ( strtolower( WJECF_Wrap( $coupon )->get_code() ) == strtolower( 'your_coupon_code' ) ) {    
            //Must have at least 3 items in the cart
            $items = WJECF_WC()->get_discount_items( $wc_discounts );
            $qty = 0;
            foreach( $items as $item_key => $item ) {
                if ( $item->product === false ) continue;
                $qty += $item->quantity;
                if ($qty >= 3) return;
            }
            throw new Exception( WC_Coupon::E_WC_COUPON_NOT_APPLICABLE );
        }
    }, 
    10, 2
);

Now the coupon will only be valid if there are at least 3 items in the cart.