# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, discounts, cart, bulk
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-quantity-discount/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Bulk Discount by Cart Quantity" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
