Repository Forums Support WooCommerce Extended Coupon Features PRO How to create coupon with multiple discount rules

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #21563
    Kit Shum
    Participant

    Hi,

    I would like to know if there’s any way to create a coupon with multiple discount rules. Currently I need to have the following discount rules:

    Cart minimum amount $1000 = 5% off all items on cart (except discount items and category B items)
    Cart minimum amount $2000 = 10% off all items on cart (except discount items and category B items)
    Cart minimum amount $3000 = 15% off all items on cart (except discount items and category B items)
    Cart minimum amount $4000 = 20% off all items on cart (except discount items and category B items)
    Cart minimum amount $5000 = 20% off all items on cart (except discount items and category B items)

    But to achieve just this alone I had to create 5 separate coupons (we also have another 30 separate coupon rules for our other discounts for the month). Is there any way to group the above discount rules into 1 coupon?

    Thank you.

    #21565
    Soft79
    Keymaster

    That would need a custom php snippet. Create a 5% discount coupon which excludes discount items and category B items. Add the following snippet to your child theme’s functions.php, replace THE_COUPON_CODE with the actual coupon code.

    
    add_filter('woocommerce_coupon_get_amount', 'custom_tiered_coupon_amount', 10, 2);
    function custom_tiered_coupon_amount($amount, $coupon) {
        if (is_admin() && !defined('DOING_AJAX')) return $amount;
        if ($coupon->get_code() !== 'THE_COUPON_CODE') {
            return $amount;
        }
        $cart_total = WC()->cart->get_subtotal();
        if ($cart_total >= 5000) {
            $amount = 20;
        } elseif ($cart_total >= 4000) {
            $amount = 20;
        } elseif ($cart_total >= 3000) {
            $amount = 15;
        } elseif ($cart_total >= 2000) {
            $amount = 10;
        } elseif ($cart_total >= 1000) {
            $amount = 5;
        } else {
            $amount = 0;
        }
        return $amount;
    }
    

    That should do the trick. I didn’t test it though.

    #21567
    Kit Shum
    Participant

    Hi,

    Many thanks that did the trick! One last question what about items based on the quantity purchased?

    (e.g.)
    buy 2 category A items, those items get 5% off
    buy 4 category A items, those items get 10% off
    buy 6 category A items, those items get 15% off
    etc etc….

    Would you be able to kindly provide the custom php snippet for this scenario? Thank you!

    #21569
    Soft79
    Keymaster

    I will get back to this…

    #21570
    Soft79
    Keymaster

    That may work with a similar script. Create a 5% discount coupon for items in category A and replace the previous snippet with the following snippet.

    Replace COUPON_CODE_B with the category B coupon code and replace COUPON_COUDE_A with the category A coupon code.

    
    add_filter(
        'woocommerce_coupon_get_amount', 
        function ($amount, $coupon) {
            if ($coupon->get_code() !== 'COUPON_CODE_B') return $amount;
            if (is_admin() && !defined('DOING_AJAX')) return $amount;
            $cart_total = WC()->cart->get_subtotal();
            if ($cart_total >= 5000) return 20;
            if ($cart_total >= 4000) return 20;
            if ($cart_total >= 3000) return 15;
            if ($cart_total >= 2000) return 10;
            if ($cart_total >= 1000) return 5;
            return 0;
        }, 10, 2
    );
    add_filter(
        'woocommerce_coupon_get_amount',
        function ($amount, $coupon) {
            if ($coupon->get_code() !== 'COUPON_CODE_A') return $amount;
            if (is_admin() && !defined('DOING_AJAX')) return $amount;
            if (!function_exists('WJECF')) return $amount;
            $n_items = WJECF()->get_quantity_of_matching_products();
            if ($n_items >= 6) return 15;
            if ($n_items >= 4) return 10;
            if ($n_items >= 2) return 5;
            return 0;
        }, 10, 2
    );
    
    #21571
    Kit Shum
    Participant

    Hi,

    Thank you!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.