Want customers to choose a fresh product without first removing items from their cart themselves? You can set WooCommerce to automatically clear the cart whenever a new product is added. This is especially useful for single-product purchases, bookings, or checkout flows where only one item should be in the cart at a time.
In this guide, you’ll learn how to use a simple WooCommerce snippet to empty the cart before adding a new product. It’s a quick way to create a smoother shopping experience and help customers avoid confusion at checkout.
Why Empty the WooCommerce Cart Before Adding a New Product?
A cart can represent very different things: a growing shopping list or a single choice the customer is still deciding on. When your store is built around one active selection, replacing the old choice automatically can make the next step feel clear and deliberate.
Before setting up this behavior, consider which parts of the buying journey it improves—and what shoppers will lose when they make a new selection.
When replacing cart contents makes sense
Automatically clearing the cart is most helpful when an order should contain one main product or booking at a time. For example, a customer choosing a consultation slot, event ticket, subscription plan, or personalized service may expect a new selection to replace the previous one rather than sit alongside it.
It can also simplify guided purchase flows. If your page presents several versions of the same offer, shoppers can compare options by adding a different one without having to find and remove the earlier item first. This makes the WooCommerce empty cart before adding new product approach useful for:
- Single-item bookings or appointment reservations
- Plan or package selectors where only one option is valid
- Product configurators that create a new cart item for each choice
- Campaign landing pages designed around one featured purchase
It is less suitable for a typical store where customers are expected to combine unrelated products. In that setting, replacing cart contents could interrupt a normal shopping trip instead of making it easier.
That distinction matters because the change is more than a small convenience: it affects what customers can carry forward as they shop. Think through how the new behavior will be communicated and what happens to the previous selection.
How this change affects shoppers and existing cart items
When a shopper adds a new product, every item already in the cart may be removed before the new one is added. That can prevent incompatible combinations, but it may also discard quantities, product options, or items the customer intended to purchase together.
To reduce surprises, explain the one-item rule near the product selector or add-to-cart button. For instance, a short note such as “Adding this option replaces your current selection” sets a clear expectation. Also check how your store handles related details, including:
- Variable products and selected variations
- Personalizations, add-ons, and custom fields
- Coupons or cart-based discounts
- Items added from other pages or product categories
Test the complete flow on desktop and mobile before enabling it for customers. A clear message and a predictable replacement rule help turn automatic cart clearing into a useful part of the experience—not an unexpected loss of the shopper’s work.
WooCommerce Empty Cart Before Adding New Product: The Snippet
Once you’ve decided that a one-item cart fits your buying flow, the next step is to make the change predictable. The snippet below runs when WooCommerce validates an add-to-cart request, so an unsuccessful request won’t wipe out a shopper’s existing selection.
First, add the basic version to your site. Then adjust its condition if the replacement rule should apply only to certain products.
Add the code to empty the cart before a product is added
This filter runs before WooCommerce adds a product to the cart. It checks that the add request is valid and that the cart contains something before emptying it.
addfilter( 'woocommerceaddtocartvalidation', 'myemptycartbeforeadd', 10, 3 );
function myemptycartbeforeadd( $passed, $productid, $quantity ) {
if ( ! $passed || ! WC()->cart || WC()->cart->isempty() ) {
return $passed;
}
WC()->cart->emptycart();
return $passed;
}Add the snippet using a trusted code snippets plugin or your child theme’s functions.php file; avoid editing a parent theme, since updates can overwrite your changes. Test with a product already in the cart, then confirm that adding another replaces it as expected.
The basic version applies store-wide, but you can narrow the rule without changing the hook. This is useful if most products should remain combinable while a particular offer or product group replaces the current cart.
Choose whether the rule applies to every product or selected products
To target specific products, add their IDs to an array and empty the cart only when the incoming product matches. Replace the original function’s condition and cart-emptying lines with this check:
$replacecartfor = array( 123, 456 ); // Replace with product IDs.
if ( inarray( absint( $productid ), $replacecartfor, true )
&& ! WC()->cart->isempty() ) {
WC()->cart->emptycart();
}Use the IDs of the products that should trigger replacement. If you sell variations, test the exact variation add-to-cart flow too; variation IDs may differ from the parent product ID, so you may need to include the relevant IDs or adjust the check to use the parent.
One more distinction helps prevent an unexpected reset: adding a product is different from changing the quantity of an item already in the cart.
Prevent the cart from clearing when customers update quantities
Keep the emptying logic attached to woocommerceaddtocartvalidation. That hook responds to add-to-cart validation; quantity changes on the cart page use a separate update flow, so this snippet won’t clear the cart just because a shopper changes an existing item’s quantity.
Avoid moving the empty-cart call into a general cart-calculation hook, which can run repeatedly during checkout and other cart refreshes. After installation, test both actions: adding a new product should apply your replacement rule, while updating a quantity should preserve the cart.
Add, Test, and Troubleshoot the Snippet
The code is in place, but a quick check before customers use it can save you from a confusing cart experience. Choose a safe place to manage the snippet, then test the buying paths your store actually offers.
Where to safely add the WooCommerce code
If you haven’t installed the code yet, use a trusted snippets plugin or your child theme’s functions.php file. A snippets plugin makes it easy to switch the behavior off without editing theme files; with a child theme, keep a copy of your changes so they’re easy to restore.
Avoid adding custom code to a parent theme, because a theme update can overwrite it. Before making changes on a live store, create a backup and, if available, test on a staging site. Check that the snippet is enabled only once—duplicating the function or hook can cause errors or unexpected behavior.
Test adding products, variations, and multiple items
A single successful test may not cover every way customers shop. Try the common paths below, including any product options or add-to-cart buttons provided by extensions.
- Add one simple product, then add a different product and confirm the new selection replaces the old one.
- Add a variable product and test more than one variation. If your targeted rule uses product IDs, check that it recognizes the IDs used for the variations.
- Try adding a product with a quantity greater than one, then confirm the requested quantity is preserved.
- Test the cart page, product page, and any AJAX or quick-add buttons your store uses.
Also confirm that updating an existing item’s quantity does not trigger replacement. These checks help verify the WooCommerce empty cart before adding new product behavior across your actual storefront, not just one product page.
Fix common issues and disable the behavior when needed
If the cart clears but the new item does not appear, check whether another plugin, product rule, or stock restriction is rejecting the add-to-cart request. If the cart does not clear, confirm the snippet is active and that its product IDs match the incoming product or variation.
When troubleshooting, temporarily disable other cart-related customizations one at a time, then retest. If you need to stop the behavior, disable or remove the snippet in your snippets plugin, or remove the related code from your child theme. Test once more afterward to confirm products can be added without replacing existing cart contents.
Make Every New Cart Selection Clear and Predictable
Automatically emptying the cart before a new product is added can simplify one-item purchases, bookings, and guided product choices. The right WooCommerce snippet makes that replacement happen only after the add-to-cart request passes validation, while leaving quantity updates alone. You can also limit the behavior to selected products if a store-wide rule isn’t right for your shop.
Before enabling it, consider whether removing existing items fits your customers’ expectations. Add a clear notice, install the code safely, and test products, variations, quantities, and add-to-cart flows on your site. With those checks in place, you can offer a smoother, more focused purchase experience—and disable the snippet whenever your store’s needs change.