Bulk Discount by Cart Quantity
Gives an automatic percentage discount when the cart contains enough items, for example 10% off 5 or more items, shown as a line in the totals.
Opens WPIDE on
Code
add_action( 'woocommerce_cart_calculate_fees', function ( $cart ) {
// Items in cart => percentage off. Highest matching tier wins.
$tiers = array(
5 => 10,
10 => 15,
);
if ( is_admin() && ! wp_doing_ajax() ) {
return;
}
$count = $cart->get_cart_contents_count();
$percent = 0;
foreach ( $tiers as $qty => $off ) {
if ( $count >= $qty ) {
$percent = $off;
}
}
if ( $percent > 0 ) {
$discount = $cart->get_subtotal() * $percent / 100;
$cart->add_fee( sprintf( 'Bulk discount (%d%%)', $percent ), -$discount );
}
} );
Notes
Edit the $tiers list. The highest tier the cart reaches is applied.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.