# Minimum Combined Quantity for a Category

> Requires customers to buy a minimum number of items from a category in total, for example any 6 wines, mixed as they like.

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

## Code

```php
$wpide_cat_min = array(
    'categories' => array( 'wine' ), // Category slugs.
    'minimum'    => 6,               // Minimum items from these categories.
    'name'       => 'wines',         // Used in the message.
);

add_action( 'woocommerce_check_cart_items', function () use ( $wpide_cat_min ) {
    $count = 0;

    foreach ( WC()->cart->get_cart() as $item ) {
        if ( has_term( $wpide_cat_min['categories'], 'product_cat', $item['product_id'] ) ) {
            $count += (int) $item['quantity'];
        }
    }

    if ( $count > 0 && $count < $wpide_cat_min['minimum'] ) {
        wc_add_notice( sprintf( 'Please add at least %1$d %2$s to your order (you have %3$d). Mix and match as you like.', $wpide_cat_min['minimum'], esc_html( $wpide_cat_min['name'] ), $count ), 'error' );
    }
} );
```

## Notes

Only carts that contain the category are checked, so other products can still be bought on their own.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Minimum Combined Quantity for a Category" 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/
