# Limit a WooCommerce cart to one item at a time

URL: https://wp24x7.com/remove-old-items-cart-new-items-added-woocommerce/
Updated: 2026-11-29
Type: blog post

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

## Key facts

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

> **Quick answer**
Prefer "Sold individually" if you only need one of the same product. For one item in total, use the `woocommerce_add_to_cart` hook, scoped by category, with a customer notice.

## When is a single-item cart the right choice?

| Case | Why one item |
|:--|:--|
| Course, membership or subscription | Bought on its own |
| Booking slot | Two bookings would clash |
| Quote or deposit | Confusing to mix with other goods |
| Landing-page funnel | One 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:

```php
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:

| Problem | Result |
|:--|:--|
| Out of stock or later validation fails | Old cart contents are gone and nothing was added |
| Customer clicks twice or reloads | Ends up with an empty cart |
| REST and AJAX paths | Behaviour 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`:

```php
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:

```php
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?

| Issue | What to do |
|:--|:--|
| Caching | Cart, checkout and My Account pages must not be page-cached; check cache rules if the mini-cart count looks wrong |
| Plugins that add items automatically | Bundles, free gifts and add-ons may add several lines; exclude them or the rule will fight them |
| Subscriptions and bookings | They 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](/woocommerce-experts), and if checkout is already misbehaving, use our [WooCommerce checkout diagnostic](/woocommerce-checkout-problems-diagnostic-order-of-checks).

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

```php
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?

| Need | Option |
|---|---|
| Stop buying two of the same item | "Sold individually" on the product |
| A single offer funnel | A checkout-focused landing page with a direct checkout link |
| Bookings or subscriptions | The booking or subscription plugin's own cart settings |
| Complex rules by role or country | A 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.

- **WooCommerce experts** Funnels and checkout built and tested. [Read more](/woocommerce-experts)
- **Checkout fix** If checkout already misbehaves. [Read more](/woocommerce-checkout-fix)

**Cart rules too risky to guess?**
A person will scope it and test it on staging.
[Post a project](/post-project)
[Call the help desk](tel:+18043754450)

## Frequently asked questions

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

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

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

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

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

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

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

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

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

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

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

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

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

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