- Why limit WooCommerce cart to one product?
- PHP snippet: allow one product in cart
- How this snippet works
- Keep quantity at one
- Limit specific products only
- Test and troubleshoot
- FAQs About WooCommerce One-Product Carts
- Does this code block the second product?
- Can cart have one product with quantity greater than one?
- Does this work with variable products?
- Can I block instead of replace?
- Conclusion
Some WooCommerce stores need customers to purchase one product per order. This is common for subscriptions, consultations, tickets, custom quotes, and products that use separate payment or fulfilment rules. Without a limit, customers can add unrelated products and create orders your workflow cannot process cleanly.
This guide shows how to woocommerce allow 1 product in cart with a small PHP snippet. It also explains what happens when customers add a second product, how to keep quantity at one, and how to apply the rule only to selected products.
Why limit WooCommerce cart to one product?
- Separate checkout flows: each product may require different fields, payment gateways, or fulfilment.
- Subscription control: customers may need one subscription product per transaction.
- Quote and booking systems: one cart item keeps custom pricing and availability predictable.
- Simple customer experience: a focused checkout reduces accidental product combinations.
PHP snippet: allow one product in cart
Add this snippet to a small custom plugin or a PHP snippets plugin. Avoid editing a parent theme because theme updates overwrite changes.
<?php
add_filter( 'woocommerce_add_to_cart_validation', 'andriy_allow_one_product_in_cart', 9999, 3 );
function andriy_allow_one_product_in_cart( $passed, $product_id, $quantity ) {
if ( ! WC()->cart->is_empty() ) {
WC()->cart->empty_cart();
}
return $passed;
}The filter runs when WooCommerce validates an add-to-cart request. If the cart already contains something, empty_cart() removes it before the new product is added. The incoming product therefore becomes the only cart item.
Tip: This code replaces the existing cart item. It does not block the second product. Tell customers what will happen with a notice or custom interface text if replacing products could surprise them.
How this snippet works
- Customer adds the first product. Cart behaves normally.
- Customer adds another product. WooCommerce runs the validation filter.
- The callback checks whether the cart is empty.
- Existing contents are removed when needed.
- WooCommerce adds the new product to the now-empty cart.
Keep quantity at one
One product in the cart does not always mean quantity one. A customer could still set quantity to three for that product. If your rule means one item total, set the product quantity limit separately in WooCommerce or enforce it during cart validation.
<?php
add_filter( 'woocommerce_add_to_cart_quantity', 'andriy_set_cart_quantity_to_one', 10, 2 );
function andriy_set_cart_quantity_to_one( $quantity, $product_id ) {
return 1;
}Limit specific products only
If only selected products need exclusive carts, add a product ID condition. Replace 123 with your product ID. This example clears the cart only when product 123 is added; adapt the condition if other products must also trigger the rule.
<?php
add_filter( 'woocommerce_add_to_cart_validation', 'andriy_exclusive_product_cart', 9999, 3 );
function andriy_exclusive_product_cart( $passed, $product_id, $quantity ) {
if ( 123 === (int) $product_id ) {
WC()->cart->empty_cart();
}
return $passed;
}Test and troubleshoot
- Test logged-out and logged-in sessions: WooCommerce can restore customer carts during login.
- Test simple and variable products: variation add-to-cart requests may use a variation ID.
- Test AJAX add-to-cart: confirm fragments and notices update after replacement.
- Check cart-dependent plugins: quote, bundle, subscription, and checkout plugins may expect multiple items.
- Clear caches: page caching should not cache cart or checkout responses.
If nothing changes, temporarily test with only WooCommerce active and a default theme. Also confirm the snippet is enabled, PHP has no syntax error, and another plugin is not removing or rebuilding the cart afterward.
FAQs About WooCommerce One-Product Carts
Does this code block the second product?
No. It removes the current cart contents, then allows the new product to be added. The result is one product, with the newest product replacing the previous one.
Can cart have one product with quantity greater than one?
Yes. Cart item count and product quantity are separate rules. Use a quantity limit when customers must buy exactly one unit.
Does this work with variable products?
Usually, yes. Test the variation selector and add-to-cart request in your theme because custom product forms can change validation behavior.
Can I block instead of replace?
Yes, but blocking requires returning false and adding a WooCommerce notice when the cart already contains an item. Replacement is shorter and better when customers should switch products.
Conclusion
The woocommerce_add_to_cart_validation filter gives you a lightweight way to allow one product in a WooCommerce cart. The main snippet empties existing contents before adding the new product, while separate conditions can handle quantity, product IDs, and more complex checkout rules.
For reliable behaviour across subscriptions, quotes, variations, and custom checkout flows, hire a WooCommerce developer to test the complete customer journey, or reach out directly.

