1

I am using the following code to auto apply a coupon when customer has $100 or more in cart.

add_action( 'woocommerce_checkout_before_order_review' , 'add_coupon_notice' );
function add_coupon_notice() { 
    $cart_total = WC()->cart->get_subtotal();
    $minimum_amount = 100;

    $currency_code = get_woocommerce_currency();
    wc_clear_notices();

    if ( $cart_total < $minimum_amount ) {
        WC()->cart->remove_coupon( '20OFF100' );
        wc_print_notice( "Get 20% off if you spend more than $$minimum_amount", 'notice' );
    } else {
        WC()->cart->apply_coupon( '20OFF100' );
        wc_print_notice( '20% off $100 or more - Discount Applied!', 'notice' );
    }

    wc_clear_notices();
}

However, I want to exclude a specific product from this $100 minimum.

The specific product is on sale, and I've checked "Exclude Sale Items" in the coupon admin screen, but the code below is ignoring that.

Why isn't the 'Exclude Sale Items' working, and/or how can I go about this?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50

1 Answers1

1

The biggest misconception in your code is that even though you checked "Exclude sale items" in the coupon admin screen, that the use of WC()->cart->get_subtotal() does not take this into account.

The solution:

  • You can use the woocommerce_before_calculate_totals hook
  • To avoid problems, use a coupon code without capital letters
  • When going through the cart content, we keep track of the total, except for the excluded products
  • Based on the current amount threshold and whether or not the coupon code has already been applied, we will display notices

So you get:

function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    // Coupon code
    $coupon_code = 'coupon1';
    
    // Total amount threshold
    $amount_threshold = 100;
    
    // Targeted product IDs, several can be entered, separated by a comma
    $targeted_product_ids = array( 30, 813 );

    // Initializing variables
    $total_amount = 0;
    $applied_coupons = $cart->get_applied_coupons();

    // Loop through cart contents
    foreach( $cart->get_cart_contents() as $cart_item ) {       
        // Excluding targeted product IDs
        if ( ! in_array( $cart_item['data']->get_id(), $targeted_product_ids ) ) {          
            // Get the cart total amount
            $total_amount += $cart_item['line_total'] + $cart_item['line_tax'];
        }
    }

    // Applying coupon
    if ( ! in_array( $coupon_code, $applied_coupons ) && $total_amount >= $amount_threshold ) {
        $cart->apply_coupon( $coupon_code );
        wc_clear_notices();
        wc_add_notice( __( 'Succeeded', 'woocommerce' ), 'notice' );        
    }
    // Buy more
    elseif ( ! in_array( $coupon_code, $applied_coupons ) && $total_amount < $amount_threshold ) {
        wc_clear_notices();
        wc_add_notice( __( 'Buy more', 'woocommerce' ), 'notice' ); 
    }
    // Removing coupon
    elseif ( in_array( $coupon_code, $applied_coupons ) && $total_amount < $amount_threshold ) {
        $cart->remove_coupon( $coupon_code );
        wc_clear_notices();
        wc_add_notice( __( 'Removed', 'woocommerce' ), 'notice' );  
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50