Search code examples
phpwordpresswoocommercecartcoupon

Conditionally apply coupons automatically for specific Product IDs and quantities


I am trying to automatically apply coupons in my WooCommerce store based on product ID and quantity conditions. My end goal is for a particular coupon to apply automatically when TWO (2) of the desired products are added to the cart, and for another coupon to app,y automatically whenever THREE (3) of the desired products are added to the cart. A single quantity of the product should have no discount. The following is the CORRECTED version of the code, which now works:

add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {

if ( !WC()->cart->is_empty() ){

    // Define HERE your Targeted Product ID and coupons codes
    $target_pid = 103;
    $coupon1 = 'soccer-sibling-2';
    $coupon2 = 'soccer-sibling-3';

    // First cart loop: Counting number of subactegory items in cart
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( $target_pid == $cart_item['data']->id ){
            // Removes any coupons in the cart already
            WC()->cart->remove_coupons();
            if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
                WC()->cart->remove_coupons();
                WC()->cart->add_discount( $coupon1 );
                wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
            } elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
               WC()->cart->remove_coupons();
                WC()->cart->add_discount( $coupon2 );
                wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
            }
            // Recalculates Cart Totals to show correct price
            WC()->cart->calculate_totals();
        }
     }
  }
}

Solution

  • Theres is a lot of errors in your code and it's a little obsolete too... I have rewrite everithing in your function and hooked it in another hook.

    Here is your revisited code:

    add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
    function conditional_auto_add_coupons() {
    
        if ( !WC()->cart->is_empty() ){
    
            // Define HERE your Targeted Product ID and coupons codes
            $target_pid = 103;
            $coupon1 = 'soccer-sibling-2';
            $coupon2 = 'soccer-sibling-3';
    
            // First cart loop: Counting number of subactegory items in cart
            foreach ( WC()->cart->get_cart() as $cart_item ){
                if( $target_pid == $cart_item['data']->id ){
                    if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
                        WC()->cart->add_discount( $coupon1 );
                        wc_add_notice( __( 'A quantity discount of <strong>5%</strong> has been added.', 'theme_domain' ), 'success' );
                    } elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
                        WC()->cart->add_discount( $coupon2 );
                        wc_add_notice( __( 'A quantity discount of <strong>10%</strong> has been added.', 'theme_domain' ), 'success' );
                    }
                }
            }
        }
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This should work and will not make your website crash, but I haven't really test it, as this is very particular.