2

I have Woocommerce cart and I successfully added a "bundle" discount that allows a $7 discount off each item in the cart if the cart quantity is 5 or more.

I also want to have coupons enabled for certain products. but I don't want to stack my bundle discount and the coupon discount on top of each other.

Here is how I am currently adding my bundle discount:

add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

$discount = 0;
$cart_item_count = $cart_object->get_cart_contents_count();

if( $cart_item_count > 4 )
    $percent = 25;


$discount -= $cart_object->get_cart_contents_count() * 7;

if( $percent > 0 )
    $cart_object->add_fee( __( "Bundle discount", "woocommerce" ), $discount, true);
}   

For example:
I have 5 items in my cart and 1 of the items is already discounted with a coupon for 35% off.
Is there a way to keep my bundle discount only on the 4 non discounted items by an applied coupon?

I don't want to stack $7 off plus 35% off. I only want the coupon to have precedence if it is applied without losing the discount on my other 4 items.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Nick
  • 1,036
  • 2
  • 14
  • 27

1 Answers1

2

Here is hooked function code, that will add for each non discounted cart items a discount $7 when there is at least a minimum of 5 items in cart:

add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_total_count = $cart_object->get_cart_contents_count();
    $cart_items_count = 0;

    // Counting non discounted cart items (by a coupon discount)
    foreach( $cart_object->get_cart() as $cart_item_key => $cart_item )
        if( $cart_item['line_subtotal'] == $cart_item['line_total'] )
            $cart_items_count += $cart_item['quantity']; 

    if( $cart_total_count > 4 ){
        $discount = $cart_items_count * 7;
        $cart_object->add_fee( __( "Bundle discount", "woocommerce" ), -$discount, true);
    }
}

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

This code is tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Dude this is exactly what I needed. I never thought of comparing the total and subtotal to create a new quantity count. Thank you so much. – Nick Sep 07 '17 at 21:12
  • This is awesome but I just realized something. If and when I do a refund it is refunding the entire price of the product(s) because the discount is only applied to the cart and not the individual item. So instead of the cart_item_count * 7 , I need subtract $7 from each individual items price. That way when I do a refund it will refund the correct amount and show up correctly for reports. – Nick Sep 07 '17 at 21:41
  • @Nick this is getting complicated … You can also refund the order line "Bundle discount" normally… – LoicTheAztec Sep 07 '17 at 21:58