2
votes

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.

1

1 Answers

2
votes

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.