Skip to content
WooCommerce

Limit a WooCommerce cart to one item at a time

Make WooCommerce replace the cart contents when a customer adds a product, with a safe hook, the edge cases and how to scope it to certain products.

By WP24×7 engineers5 min readPublished August 10, 2020Updated November 29, 2026

At a glance

To leave a WooCommerce customer with exactly one item in the cart, hook woocommerce_add_to_cart, which runs after a successful add, and remove every other cart line. Limit it to the products or categories that need it, tell the customer what happened and test stock failures, variations, block carts and caching. This guide gives safe code, edge cases and alternatives.

Updated November 29, 2026

  • For store owners and developers selling courses, bookings or single-offer funnels
  • Hook woocommerce_add_to_cart, after a successful add
  • Scope limit to certain products or categories
  • Tell the customer a notice when an item is removed
  • No-code option Sold individually, for the same product only
  • Test variations, stock failure, classic and block cart, mini-cart, guests
  • Help WooCommerce experts

A "Buy now" button that should leave the customer with exactly one thing in the cart is a common request for courses, bookings and single-product funnels. Hook `woocommerce_add_to_cart`, remove every other cart line, and limit it to the products that need it.

When is a single-item cart the right choice?

CaseWhy one item
Course, membership or subscriptionBought on its own
Booking slotTwo bookings would clash
Quote or depositConfusing to mix with other goods
Landing-page funnelOne offer
Adding a second product replaces the first, with a notice

For an ordinary shop it is frustrating, because customers lose items they added earlier without warning. If you only need to stop customers buying more than one of the same product, use WooCommerce's "Sold individually" option on the product instead, which needs no code.

What is the simplest safe code?

Put this in a small site plugin or a child theme's functions.php, after taking a backup:

add_action( 'woocommerce_add_to_cart', function ( $cart_item_key ) {
	$cart = WC()->cart;
	if ( ! $cart ) {
		return;
	}

	foreach ( $cart->get_cart() as $key => $item ) {
		if ( $key !== $cart_item_key ) {
			$cart->remove_cart_item( $key );
		}
	}
}, 10, 1 );

woocommerce_add_to_cart runs after the product has been added successfully, and its first argument is the key of the new cart line. The loop keeps that line and removes the rest.

Why not empty the cart in the validation filter?

A common snippet empties the cart inside woocommerce_add_to_cart_validation, which runs before WooCommerce decides whether the item can be added. The problems:

ProblemResult
Out of stock or later validation failsOld cart contents are gone and nothing was added
Customer clicks twice or reloadsEnds up with an empty cart
REST and AJAX pathsBehaviour is hard to predict

Running after a successful add avoids all three. If you must stay in the validation filter, only empty the cart when $passed is true, and understand that a later failure can still strand the customer with an empty cart.

How do I limit it to certain products?

Check the product before removing anything. This version applies only to products in a category called courses:

add_action( 'woocommerce_add_to_cart', function ( $cart_item_key, $product_id ) {
	if ( ! has_term( 'courses', 'product_cat', $product_id ) ) {
		return;
	}
	$cart = WC()->cart;
	foreach ( $cart->get_cart() as $key => $item ) {
		if ( $key !== $cart_item_key ) {
			$cart->remove_cart_item( $key );
		}
	}
}, 10, 2 );

The hook passes the product ID as its second argument, and has_term() works on product categories because they are a normal taxonomy (product_cat). Variation adds also pass the parent product ID here, so a category set on the parent applies to its variations.

You may also want the reverse case: if the cart already holds a "courses" item, adding something else should replace it. Do that by checking the existing lines with has_term() as well. Keep the logic obvious, because every extra rule is another way for a customer to lose an item.

Should I tell the customer what happened?

Yes. A silent removal looks like a bug. Add a notice:

wc_add_notice( __( 'Your cart was updated: this product is bought on its own.', 'my-site' ), 'notice' );

Add it only when something was actually removed, which means setting a flag inside the loop.

What should I test?

  1. Add

    item A, then item B: only B remains, the notice shows, the cart total is right.

  2. Add

    the same item twice: the quantity increases, nothing odd is removed.

  3. Variable product

    choose attributes and add.

  4. Out-of-stock or invalid add

    the old cart should still be there.

  5. Both

    the classic cart shortcode and the block cart if your theme uses them.

  6. Mini-cart

    AJAX updates, and the checkout page when a coupon is applied.

  7. A logged-in

    customer with a saved cart, and a guest.

Check the cart count in the header after each add, since cached fragments sometimes lag.

What goes wrong in practice?

IssueWhat to do
CachingCart, checkout and My Account pages must not be page-cached; check cache rules if the mini-cart count looks wrong
Plugins that add items automaticallyBundles, free gifts and add-ons may add several lines; exclude them or the rule will fight them
Subscriptions and bookingsThey have their own cart rules; check what they already offer first

If your funnel or checkout is more complex than a snippet can safely handle, see our WooCommerce specialists, and if checkout is already misbehaving, use our WooCommerce checkout diagnostic.

Worked example: a single-course checkout

Imagine a site selling online courses. A visitor clicks "Enrol" on course A, changes their mind and clicks "Enrol" on course B. Without a rule, the cart shows both and the customer may pay for something they did not mean to buy. With the category rule above, adding B removes A, a notice says the cart was updated, and the cart shows one course. If you also want to send the customer straight to payment, WooCommerce provides a redirect filter:

add_filter( 'woocommerce_add_to_cart_redirect', function ( $url ) {
	return wc_get_checkout_url();
} );

This sends every add-to-cart to checkout, so scope it to your single-item products in the same way, by checking the product first. In classic AJAX add-to-cart on archive pages, the redirect depends on the "Redirect to the cart page after successful addition" setting, so test the button you actually use.

What are the alternatives to code?

NeedOption
Stop buying two of the same item"Sold individually" on the product
A single offer funnelA checkout-focused landing page with a direct checkout link
Bookings or subscriptionsThe booking or subscription plugin's own cart settings
Complex rules by role or countryA maintained rules plugin, or custom development

Prefer a setting to a snippet where one exists, because settings survive updates and are visible to the next person.

A final warning on custom cart code

Cart code runs on every shopping session, so a mistake affects every customer at once. Test on staging with the same plugins as production, deploy outside busy hours, and keep the snippet in one clearly named place with a comment saying why it exists. If sales drop after the change, remove it first and investigate second.

Cart rules too risky to guess?

A person will scope it and test it on staging.

FAQ

Questions people ask

14 answers. Can't find yours? Ask an engineer.

Will this break stores that sell several products?

Yes, if applied to every product. Limit the rule to the products or categories that should be bought alone, such as a course, a booking or a subscription.

Link to this answer
Why not use woocommerce_add_to_cart_validation?

It runs before the item is accepted. If a later check fails, for example stock, the cart has already been emptied and the new item was not added.

Link to this answer
Does this work with the Store API and block checkout?

The woocommerce_add_to_cart action also runs for cart changes made through WooCommerce's cart code, but test both the classic and block cart on your own site.

Link to this answer
Can I send customers straight to checkout after adding?

Yes, with the woocommerce_add_to_cart_redirect filter, or the redirect setting in WooCommerce. Limit it to your single-item products and test the button type you use.

Link to this answer
When is a single-item cart the right choice?

For a course, membership or subscription bought on its own, a booking slot where two bookings would clash, a quote or deposit or a landing-page funnel with one offer. For an ordinary shop it frustrates customers.

Link to this answer
Is there a no-code way to stop buying two of the same product?

Yes. Use WooCommerce's Sold individually option on the product. It needs no code and survives updates.

Link to this answer
Why not empty the cart in woocommerce_add_to_cart_validation?

It runs before WooCommerce decides whether the item can be added. If stock or another validation fails afterwards, the old cart is gone and nothing was added.

Link to this answer
How do I limit the rule to certain products?

Check the product before removing anything. The hook passes the product ID as its second argument, and has_term works on the product_cat taxonomy. Variation adds pass the parent product ID, so a parent category applies to its variations.

Link to this answer
Should I tell the customer when an item is removed?

Yes. A silent removal looks like a bug. Add a wc_add_notice message, only when something was actually removed, by setting a flag inside the loop.

Link to this answer
What should I test before going live?

Adding A then B, adding the same item twice, a variable product, an out-of-stock add, classic and block carts, mini-cart AJAX, checkout with a coupon and both logged-in and guest customers.

Link to this answer
Can I send customers straight to checkout after adding?

Yes, with the woocommerce_add_to_cart_redirect filter or the WooCommerce redirect setting. Scope it to your single-item products and test the button type you use, since AJAX add-to-cart on archive pages depends on the setting.

Link to this answer
What can go wrong with this rule in practice?

Page caching of cart and checkout, plugins that add several lines in one click such as bundles and free gifts, and subscription or booking plugins with their own cart rules.

Link to this answer
Does this work with the block cart and Store API?

The woocommerce_add_to_cart action also runs for cart changes made through WooCommerce's cart code, but test both the classic and block cart on your own site.

Link to this answer
What if sales drop after I add the code?

Remove the snippet first and investigate second. Cart code runs on every shopping session, so test on staging with the same plugins as production and deploy outside busy hours.

Link to this answer

Keep reading

Want an engineer on it?

Tell us what is wrong in plain words. A person reads it, today.