Fixing WooCommerce add to cart not working (or the cart emptying)
How to fix WooCommerce add to cart buttons that do nothing, spin forever or leave the basket empty, and baskets that empty between pages. Covers product settings, caching, cookies, JavaScript errors, blocked requests and plugin conflicts.
A customer clicks Add to cart (or Add to basket, depending on your theme and language) and nothing happens. Or the button spins forever. Or the page reloads with an "added" message but the basket is empty. Sometimes the item goes in, then vanishes when they move to another page or reach the checkout.
These all come down to the same thing: WooCommerce can't save or remember what's in the customer's basket. It keeps each basket in your database and finds it again using a cookie in the customer's browser.1 Anything that stops the add to cart request getting through, or stops that cookie being set and sent back, breaks the basket.
The steps below start with the quick checks and work towards the more involved ones.
Check the product can be bought
Start here if only some products are affected. WooCommerce only lets customers buy a product that's published and has a price.2 An empty Regular price field on the General tab of the product is enough to stop it. On shop and category pages, a simple product with no price, or one that's out of stock, shows a Read more button instead of Add to cart.3
Check the Inventory tab too. If Stock status says Out of stock, or stock management is switched on and the quantity is zero with backorders not allowed, WooCommerce refuses to add it and shows a "cannot add ... because the product is out of stock" message.4 If Sold individually is ticked, customers can only have one in the basket, and a second add is refused with a "You cannot add another" message.4
Test logged out, because administrators can buy draft and private products that customers can't.2
For variable products, the customer has to pick an option from every dropdown, and the chosen variation needs its own price and stock. Our guide to fixing WooCommerce variations not showing covers greyed out buttons and "no products matched your selection" errors.
Check the add to cart settings
Go to WooCommerce→ Settings→ Products and look at Add to cart behaviour on the General section. There are two tick boxes:5
| Setting | What it does |
|---|---|
| Redirect to the cart page after successful addition | Sends the customer straight to the basket after adding a product. Off by default. |
| Enable AJAX add to cart buttons on archives | Lets buttons on shop and category pages add products without reloading the page. On by default. |
The AJAX option only affects buttons in product listings (shop and category pages, and product grids elsewhere), not the single product page. When it's on, WooCommerce loads a script called wc-add-to-cart to handle those buttons.6 On a classic theme, the button on a single product page sends a normal form and reloads the page instead.7
That gives you a handy test. If shop page buttons spin but the product page works, the problem is almost certainly JavaScript or a blocked request (see below). Unticking the AJAX option is a reasonable stopgap meanwhile.
Also check WooCommerce→ Settings→ Advanced→ Page setup has a Cart page chosen, and that the page is still published.
Rule out page caching
This is the cause I see most often, especially when the basket empties between pages or shows someone else's items.
A page cache saves a copy of each page and serves it to everyone. That's fine for blog posts but breaks the basket, checkout and My Account pages, which are different for every customer. WooCommerce's advice is to exclude those three pages from every cache, along with these cookies:8
woocommerce_items_in_cartwoocommerce_cart_hashwp_woocommerce_session_(followed by a code unique to your site)
WooCommerce asks caching plugins not to cache those pages, and many respect that.9 Caches outside WordPress don't always listen. Check each layer you have:
- Your caching plugin. Most have settings to exclude pages and cookies. Check the three pages and the cookies above are in them.
- Your host's server cache. Server caches are common on managed hosting, and with Varnish or NGINX caching you usually can't see the settings. Ask your host to confirm the WooCommerce pages and cookies are excluded. WooCommerce's caching guide has example Varnish rules you can send them.8
- Your CDN. Cloudflare doesn't cache normal pages by default.10 A "cache everything" rule changes that, and Cloudflare warns it can show visitors information not meant for them.11 If you have one, add exceptions for the WooCommerce pages and cookies, or remove it.
Once you've changed the rules, clear every cache. Our guide to a WordPress site not updating walks through each layer. Then test in a private window: add something, visit a few other pages and go back to the basket. Many caches skip logged-in users, which is why this often only affects customers.
Check your site address and cookies
Under Settings→ General, check that the WordPress Address (URL) and Site Address (URL) match the address customers actually use, including https:// and whether there's a www. By default WordPress sets cookies for the exact address being visited,12 so a cookie set on www.example.com isn't sent to example.com. If links or redirects bounce customers between the two, the basket appears to empty. The same mismatch can stop you logging in, as our guide to the "Cookies are blocked" error explains.
On a site whose address starts with https://, WooCommerce marks its basket cookie as secure, and browsers only send secure cookies over HTTPS.113 Any page still loading over plain http:// shows an empty basket. Our guides to fixing "Not Secure" warnings and fixing redirect loops cover fixing both. Note down the current addresses before changing them. A wrong change can lock you out, and changing the address signs everyone out and empties guests' current baskets, so do it at a quiet time.
Cookie consent plugins are the other usual suspect. Some hold back cookies or scripts until the visitor accepts, and if WooCommerce's are caught up in that, the basket breaks for anyone who hasn't accepted. Test by declining cookies in a private window. If that breaks the basket, check the consent tool's settings or ask its support team. Which cookies need consent is a legal question, so take advice if you're unsure.
The sessions table
Baskets live in a database table called woocommerce_sessions (usually with wp_ in front).1 The Database section of WooCommerce→ Status→ System status shows "Table does not exist" if it's missing.14 If so, take a backup and use Verify database under WooCommerce→ Status→ Tools, which tries to recreate missing tables.15
A guest's basket is kept for two days by default. A logged-in customer's session lasts a week, and their basket is also saved to their account, so it comes back when they next log in.1 WooCommerce clears expired ones twice a day using scheduled actions,16 so if those aren't running the table can grow very large. Our guide to past-due scheduled actions covers fixing that.
Don't use Clear customer sessions on the Tools screen as a fix. It deletes every customer's current basket, including saved baskets.15
Look for JavaScript errors
If the button spins forever or does nothing at all, look for JavaScript errors. On a page where the button fails, open your browser's developer tools (F12, or right-click and choose Inspect) and select the Console tab. Click the button and look for errors in red. One error from any plugin can stop WooCommerce's scripts running, and the file path in the error (/plugins/plugin-name/) usually points at the plugin responsible. Our guide to fixing "not a valid JSON response" explains the developer tools in more detail.
Optimisation plugins are a frequent cause. Settings that combine, minify, defer or delay JavaScript can break WooCommerce's scripts, particularly wc-add-to-cart, wc-add-to-cart-variation (for variable products) and wc-cart-fragments, which keeps a classic theme's mini-cart count up to date.617 Switch off the plugin's JavaScript settings, clear its cache and test again. If that fixes it, add the WooCommerce scripts to its exclusion list and switch the settings back on.
A mini-cart showing the wrong count is often the same problem, or the header holding it is being served from a cache.
Outdated theme templates
Classic themes can override WooCommerce's templates with their own copies, and old copies can be missing things newer versions expect. Scroll to the Templates section of WooCommerce→ Status→ System status. Out of date files carry a warning that the version "is out of date", and an Outdated templates line appears at the bottom.14
The usual fix is an updated theme. Customised templates need updating by hand, which WooCommerce's guide explains.18 Back up your theme before updating it or changing any template files, since an update overwrites any edits made directly to it.
Check the request isn't being blocked
Clicking an AJAX add to cart button sends a background request to your site, ending in ?wc-ajax=add_to_cart.19 Block-based shops send theirs to addresses starting /wp-json/wc/store/ instead,20 and some themes and plugins use WordPress's admin-ajax.php for their own buttons. If a security plugin or firewall blocks that request, the button spins and nothing is added.
To check, open developer tools, select the Network tab, then click the button. Find the request for add_to_cart, add-item or batch and look at the status number. 200 means it got through; if the click then jumps to the product page, WooCommerce refused the item and the product page shows why. A 5xx number means a server error (see the logs section below). A 403 means something refused it. On a block-based shop, if the response mentions "Nonce is invalid", WooCommerce itself rejected it, usually because the page came from a cache. Otherwise a security plugin or firewall is the likely cause, and our guide to fixing 403 forbidden errors covers those.
If you use Cloudflare, check its security events for blocked requests at the time you clicked. Your host can check their own firewall for you.
Rule out plugin and theme conflicts
If nothing above has found it, another plugin or your theme is the likely cause. Suspect plugins that add product options, change prices or add their own mini-cart first.
Deactivating plugins on a live shop switches their features off for customers, so do this on a staging copy if you can. If you have to test on the live site, the Health Check & Troubleshooting plugin's troubleshooting mode disables all plugins and switches to a default theme for your login only, while customers see the normal site.21 It hasn't been updated for a while, so check it works on your site first. Turn plugins back on one at a time (WooCommerce first) and test the basket after each. It tests while you're logged in, so it won't show caching problems.
To test your theme, switch to Storefront (WooCommerce's own free theme) or a default WordPress theme on your staging copy.22 If the basket works there, the problem is in your theme. Switching themes on a live site changes how it looks for everyone, and you'd need to reactivate your theme and check your menus and widgets afterwards, so staging is much safer. When you're done, check each plugin is active again under Plugins.
Check which cart your shop uses
WooCommerce has two kinds of basket page. The older one uses a shortcode and PHP templates. The newer one uses the Cart and Checkout blocks, the default for new shops since WooCommerce 8.3.23 Open your basket page in the editor to see which you have.
The blocks talk to WooCommerce through its Store API, so anything blocking /wp-json/ addresses breaks them.20 Some older plugins that change the basket only work with the shortcode version, so if the basket broke when you switched to the blocks, check each plugin's documentation for block support.
Check the logs
If the Network tab shows a 500 status, PHP has hit an error. WooCommerce records fatal errors under WooCommerce→ Status→ Logs, with the source "fatal-errors", even when WordPress debugging is off.24 Look for entries from the time you tested.
For more detail, switch on WordPress's debug log and try adding to the basket again. Our guide to the WordPress debug log explains how, including backing up wp-config.php before editing it. Switch debugging off and delete debug.log when you've finished.
Common questions
Will customers lose their baskets while I fix this?
Not usually. Changing cache rules, plugin settings or your theme doesn't touch the baskets saved in your database. The exceptions are the Clear customer sessions tool, which empties every basket, and changing your site address, which can sign people out and change the basket cookie.
Why does the basket work for me but not for customers?
Usually because you're logged in. Many caches skip logged-in users, so you get fresh pages while customers get cached copies. Administrators can also add draft and private products that customers can't. Always test in a private window while logged out, and try a phone on mobile data too, which rules out anything left over in your own browser.
Is my host's caching causing the basket to empty?
It often is, especially on managed WordPress hosting with its own server cache. You can't always see those settings, so ask your host to confirm the basket, checkout and My Account pages and the WooCommerce cookies are excluded. Pointing them to WooCommerce's caching guide helps. Hosts that sell WooCommerce hosting usually know these rules and can check quickly.
Is it safe to turn off AJAX add to cart buttons?
Yes. Unticking it means shop and category page buttons reload the page instead of adding in the background. Nothing is lost and you can switch it back on at any time. It can get customers buying again while you track down a JavaScript problem, though it won't fix a basket that empties because of caching or cookies.
Still losing baskets?
If customers still can't add to the basket after these steps, or you're losing sales and need it sorted quickly, my emergency WordPress support can find the cause. Many fixes are £50 all in, you'll get a fixed quote before I start and it's no fix, no fee. If customers get as far as the checkout but can't finish, see our guides to WooCommerce checkout not working and shipping options not showing.
class-wc-session-handler.php, WooCommerce source code on GitHub. ↩ ↩ ↩ ↩
abstract-wc-product.php (is_purchasable and is_viewable), WooCommerce source code on GitHub. ↩ ↩
class-wc-product-simple.php (add_to_cart_text), WooCommerce source code on GitHub. ↩
class-wc-cart.php (add_to_cart), WooCommerce source code on GitHub. ↩ ↩
class-wc-settings-products.php, WooCommerce source code on GitHub. ↩
class-wc-frontend-scripts.php, WooCommerce source code on GitHub. ↩ ↩
single-product/add-to-cart/simple.php, WooCommerce source code on GitHub. ↩
How to configure caching plugins for WooCommerce, WooCommerce Developer Docs. ↩ ↩
class-wc-cache-helper.php (prevent_caching), WooCommerce source code on GitHub. ↩
Default cache behavior, Cloudflare Docs. ↩
Cache Level (Cache Everything), Cloudflare Docs. ↩
default-constants.php (COOKIE_DOMAIN), WordPress source code on GitHub. ↩
wc-conditional-functions.php (wc_site_is_https), WooCommerce source code on GitHub. ↩
html-admin-page-status-report.php, WooCommerce source code on GitHub. ↩ ↩
class-wc-rest-system-status-tools-v2-controller.php, WooCommerce source code on GitHub. ↩ ↩
class-woocommerce.php (woocommerce_cleanup_sessions), WooCommerce source code on GitHub. ↩
class-wc-widget-cart.php, WooCommerce source code on GitHub. ↩
How to fix outdated WooCommerce templates, WooCommerce Developer Docs. ↩
class-wc-ajax.php (get_endpoint and add_to_cart), WooCommerce source code on GitHub. ↩
Health Check & Troubleshooting, WordPress.org Plugin Directory. ↩
Storefront: Official theme for WooCommerce, WooCommerce on GitHub. ↩
WooCommerce 8.3.0 Released, WooCommerce Developer Blog, 16 November 2023. ↩
class-woocommerce.php (log_errors), WooCommerce source code on GitHub. ↩