• Resolved autifsnuc

    (@autifsnuc)


    I’m trying to implement code that will make it so that users can enter a coupon code which will only work once they’ve added a certain quantity or greater to their cart. The code that I have so far is based off of this tutorial:

    https://coding.mangopear.co.uk/resources/automatically-apply-woocommerce-coupons-based-on-cart-quantity/

    Here is my code:

    <?php
    
    function is_individual_coupon_applied() {
        global 	$woocommerce; // [a]
    
        $coupon_args = array(
            'posts_per_page'	=> -1,
            'orderby'			=> 'title',
            'order'				=> 'asc',
            'post_type'			=> 'shop_coupon',
            'post_status'		=> 'publish',
            'meta_key'			=> 'individual_use',
            'meta_value'		=> 'yes',
        );
    
        $coupons = get_posts($coupon_args); // [b]
    
        foreach ($coupons as $coupon) : // [c]
            $coupon_title = get_the_title($coupon); // [d]
    
            if ($woocommerce->cart->has_discount($coupon_title)) : return true; // [e][f]
            else : return false; // [e][g]
            endif;
        endforeach;
    }
    
    function bulk_discount_coupons() {
        global 	$woocommerce; // [a]
    
        if (is_cart() || is_checkout())  {
            if (is_individual_coupon_applied() != true){
                $coupon_five_percent = '5% discount'; // [b]
                $coupon_ten_percent  = 'TOOMANYNUCS'; // [b]
                $cart_contents_count = $woocommerce->cart->cart_contents_count; // Get cart contents
    
                if ($woocommerce->cart->has_discount($coupon_ten_percent)) {
                    $woocommerce->cart->remove_coupon($coupon_ten_percent);
                }
    
                if ($cart_contents_count >= 10) {
                    $woocommerce->cart->add_discount($coupon_ten_percent);
                } else {
                    echo "less than 10";
                }
            }
        }
    }
    
    add_action('wp_head', 'bulk_discount_coupons');
    

    This code works as far as if the quantity is less than 10, the coupon is no longer applied and the total cart amount reflects. If the quantity is 10 or greater, the coupon is automatically applied and the total cart amount reflects. It works however, I don’t want the coupon to apply automatically. I just want it to be enabled.

    With how the code is right now, the user isn’t required to enter the coupon code to receive the discount. Please help me figure out how to enable the coupon but not have it apply automatically. Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Riaan K.

    (@riaanknoetze)

    Hi there,

    Out of interest, why don’t use the default coupons in WooCommerce by setting a minimum spend amount necessary (equivalent to the value that 10 products would have)? You could further limit that to *specific* products if you have multiple products in the store which isn’t eligible.

    I’m asking as that would mean you wouldn’t need to fiddle with 3rd party custom code 🙂

    Thread Starter autifsnuc

    (@autifsnuc)

    Because if product X costs $3 and product Y costs $5, the solution will only work in one of those cases. It isn’t an efficient solution.

    If i can make a coupon quantity based, it will work regardless of the times 10 calculated cost and therefore, regardless of the particular product. One coupon for many instead of many coupons for many products.

    Also, i cannot use any additional plugins. That’s why I’m seeking a programmatic solution.

    • This reply was modified 6 years, 7 months ago by autifsnuc.
    Plugin Support Riaan K.

    (@riaanknoetze)

    Thanks for clarifying that – admittedly it was entirely clear in the initial thread 🙂

    Given that this is a fairly complex development topic, I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the following places for more development-oriented questions:

    1. WooCommerce Slack Community: https://woocommerce.com/community-slack/
    2. Advanced WooCommerce group on Facebook: https://www.facebook.com/groups/advanced.woocommerce/
    con

    (@conschneider)

    Engineer

    Hi there,

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

    Kind regards,

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Programmatic Quantity Based Coupons’ is closed to new replies.