When add to cart breaks, the site is up and the store is down, so triage beats theory. Open the browser network panel, filter on wc-ajax, and click the button once. That single request sorts the incident into one of three buckets in about ten seconds: no request fires at all, which is a front-end script or a product-configuration problem; a request fires and returns an error, which is PHP or stock rules; or a request succeeds and the cart still reads empty on the next page, which is always the session and cookie layer, usually with a page cache or CDN standing in the middle of it. Everything below follows that split.
What you’ll accomplish
- Classify the failure in one request instead of guessing at plugins.
- Reproduce the add to cart headlessly with
curlso you can separate a server-side failure from a browser-side one. - Read the WooCommerce session out of the database and the object cache, and prove whether it is being written at all.
- Fix the cookie, cache and CDN rules that let a cart evaporate between two page loads.
- Verify the repair end to end, with a cart that survives a fresh request rather than a settings screen that looks correct.
Prerequisites
WooCommerce 9.x or 10.x on WordPress 6.5 or newer, PHP 8.1 or later, MySQL 8 or MariaDB 10.6. Shell access with WP-CLI available as wp, run as the site user from the WordPress root. Administrator access to the store, a browser with developer tools, and curl on a machine that can reach the site over the public hostname rather than through an internal bypass — half the causes in this article only appear when the request crosses the CDN.
Two assumptions shape the commands. First, every wp wc subcommand needs a --user= flag naming an administrator, because the WooCommerce REST controllers behind them run capability checks; --user=1 is used throughout and you should swap in your own admin ID. Second, the examples assume the default wp_ table prefix. Run wp config get table_prefix and substitute if yours differs, otherwise every SQL statement below returns a table-not-found error that has nothing to do with your incident.
Have a real, simple, in-stock product ID ready as a control. A variable product makes a poor first test because it fails for its own reasons.
How it works
There are two add to cart paths and they fail differently. The classic path is a plain form POST carrying add-to-cart=<id>, handled by WC_Form_Handler::add_to_cart_action() on wp_loaded, followed by a redirect. The AJAX path — the default on archive and shop pages since the “add to cart behaviour” options were introduced — posts to the WooCommerce front controller at /?wc-ajax=add_to_cart, handled by WC_AJAX. The same events are also registered on admin-ajax.php as woocommerce_add_to_cart, which is why some themes and older plugins post there instead. Both endpoints exist, both are valid, and knowing which one your theme uses tells you which URL to watch.
The browser side depends on two scripts. wc-add-to-cart.js binds the button and is localised with wc_add_to_cart_params, which carries ajax_url, wc_ajax_url and cart_url. wc-cart-fragments.js refreshes the mini cart by calling get_refreshed_fragments and caching the result in sessionStorage. If either script fails to enqueue, or jQuery throws before the handler binds, the click submits nothing and no request appears in the network panel at all.
The server side depends on the session. WC_Session_Handler issues a cookie named wp_woocommerce_session_ plus the site’s COOKIEHASH, whose value packs the customer ID, an expiry, and a hash. The cart itself lives in the wp_woocommerce_sessions table, keyed by session_key, and is mirrored into the object cache group wc_session_id when a persistent cache such as Redis is present. WC_Cart additionally sets two unauthenticated cookies, woocommerce_items_in_cart and woocommerce_cart_hash, which exist so a page cache can tell “this visitor has a cart” without reading the session. Those two cookies are the ones cache-bypass rules key on, and they are the ones a badly configured CDN most often strips.
That gives you the failure map: no cookie means the session never anchored; a cookie with no matching row means the write failed or was rolled back; a row that exists but returns the wrong cart means the object cache handed back a stale entry.
Step-by-step
1. Get a control product and confirm it is actually purchasable. Half of these tickets are configuration, not breakage.
wp wc product get 123 --user=1 --fields=id,name,type,status,stock_status,manage_stock,stock_quantity,backorders,sold_individually
+-------------------+---------------+
| Field | Value |
+-------------------+---------------+
| id | 123 |
| name | Cedar Bench |
| type | simple |
| status | publish |
| stock_status | instock |
| manage_stock | 1 |
| stock_quantity | 14 |
| backorders | no |
| sold_individually | false |
+-------------------+---------------+
A stock_status of outofstock with backorders set to no is a working store refusing a sale, not a bug. sold_individually set to true means a second click legitimately does nothing when the item is already in the cart. For a variable product, list the children and check that at least one is purchasable.
wp wc product_variation list 123 --user=1 --fields=id,sku,stock_status,purchasable
2. Watch one real click in the network panel. Open developer tools, select Network, filter on wc-ajax, click add to cart once, and read the row.
POST https://example.com/?wc-ajax=add_to_cart
Status: 200
Request payload: product_id=123&quantity=1
Response: {"fragments":{"div.widget_shopping_cart_content":"..."},"cart_hash":"5c7b0d9a1f"}
No row at all means the JavaScript never ran: switch to the Console tab and read the first error, not the last. A 403 usually means an expired nonce served from cached HTML or a security plugin refusing the POST. A 500 sends you to the PHP error log. A 200 with an empty cart_hash means the item was rejected server-side, and WooCommerce will have put the reason in the response notices.
3. Reproduce the request without a browser. This is the step that ends most arguments, because it removes the theme, the extensions and the browser cache from the picture.
curl -sS -o /tmp/atc.json -D /tmp/atc.headers -c /tmp/wc-cookies.txt \
-A 'Mozilla/5.0 (cart-triage)' \
--data 'product_id=123&quantity=1' \
'https://example.com/?wc-ajax=add_to_cart'
head -c 120 /tmp/atc.json; echo; grep -i -E '^(set-cookie|cf-cache-status|x-cache|age):' /tmp/atc.headers
{"fragments":{"div.widget_shopping_cart_content":"<ul class=\"woocommerce-mini-cart
set-cookie: woocommerce_items_in_cart=1; path=/; secure
set-cookie: woocommerce_cart_hash=5c7b0d9a1f2e4b6c; path=/; secure
set-cookie: wp_woocommerce_session_1b0f9c=t_5f3a9c%7C%7C1756000000%7C%7C1755996400%7C%7Cbd41; path=/; secure
Three Set-Cookie headers is the healthy signature. If they are missing here but the JSON is fine, something between PHP and your terminal is discarding them — go to step 6. If cf-cache-status: HIT or a non-zero age appears on this response, the edge is caching a POST route it must never cache, and that alone explains a cart that behaves randomly for different visitors.
4. Prove the session persists across a second request. Replay with the jar you just collected.
curl -sS -b /tmp/wc-cookies.txt 'https://example.com/?wc-ajax=get_refreshed_fragments' | head -c 160
{"fragments":{"div.widget_shopping_cart_content":"<ul class=\"woocommerce-mini-cart cart_list\"><li class=\"woocommerce-mini-cart-item
A mini cart containing your product means the server side is healthy end to end, and the fault is in the browser or at the edge. An empty mini cart on a valid cookie means the session is not being read back, which is step 5.
5. Look at the session store directly. The cart is a row, so go and look at the row.
wp db query "SELECT COUNT(*) AS live_sessions, FROM_UNIXTIME(MAX(session_expiry)) AS newest FROM wp_woocommerce_sessions;"
wp db query "SELECT session_key, FROM_UNIXTIME(session_expiry) AS expires, LENGTH(session_value) AS bytes FROM wp_woocommerce_sessions ORDER BY session_expiry DESC LIMIT 5;"
live_sessions newest
418 2026-08-24 11:42:07
session_key expires bytes
t_5f3a9c 2026-08-24 11:42:07 1184
A count that stays at zero while you add items means writes are failing: check that the table exists, that the database user has INSERT on it, and that the site is not read-only after a migration. A count in the hundreds of thousands means the woocommerce_cleanup_sessions cron event is not running, and the table has grown until writes crawl.
wp cron event list --fields=hook,next_run_relative --format=table | grep -i session
6. Rule the object cache in or out. A persistent cache in front of the session table is the classic cause of a cart that shows the wrong contents rather than no contents.
wp redis status
wp cache flush
Status: Connected
Client: PhpRedis (v6.0.2)
Drop-in: Valid
Success: The cache was flushed.
If flushing fixes it for a few minutes and the problem returns, inspect the eviction policy. A Redis instance running allkeys-lru against a small maxmemory will silently evict wc_session_id entries under load, and shoppers lose carts at random times of day. Session data belongs in a database-backed store with the cache as an accelerator, never in an instance configured to throw keys away.
7. Fix the cache and CDN rules, at both layers. Cart, checkout and account pages must never be served from a full-page cache, and the WooCommerce cookies must force a bypass everywhere else.
# Bypass the page cache for cart, checkout, account and the WooCommerce AJAX router
if ($request_uri ~* "/(cart|checkout|my-account)/") { set $skip_cache 1; }
if ($request_uri ~* "wc-ajax=") { set $skip_cache 1; }
if ($http_cookie ~* "woocommerce_items_in_cart|woocommerce_cart_hash|wp_woocommerce_session_") { set $skip_cache 1; }
Reload and confirm the bypass is real rather than declared.
nginx -t && systemctl reload nginx
curl -sSI -b /tmp/wc-cookies.txt 'https://example.com/cart/' | grep -i -E '^(x-cache|cf-cache-status|cache-control):'
cache-control: no-cache, no-store, must-revalidate
x-cache: BYPASS
The same rules have to exist at the CDN, and the CDN must be told to pass those cookies through instead of stripping them for cache efficiency. Our WooCommerce caching rules article covers the full rule set, and WooCommerce cache bypass cookies covers the edge-side configuration in detail.
8. Settle the hostname and scheme question. Cookies are scoped to a host and a protocol, so a mismatch produces a cart that exists on one URL and not the other.
wp option get home
wp option get siteurl
wp config get COOKIE_DOMAIN
https://example.com
https://example.com
Error: The constant 'COOKIE_DOMAIN' is not defined.
That undefined constant is the correct answer for a single-hostname store; WordPress derives the domain from home and everything lines up. Trouble starts when home says example.com, the CDN also answers on www.example.com, and shoppers land on whichever one a stale link gave them: the session cookie set on one host is invisible on the other. Pick one canonical hostname, redirect the other at the edge with a 301, and only define COOKIE_DOMAIN if you genuinely need to share a session across subdomains. The same reasoning applies to scheme — a page served over http cannot read a cookie marked secure, so a partial HTTPS migration empties carts for exactly the visitors who arrive on the old scheme.
9. If the request still never fires, bisect the plugins. Do this last, on staging, and record the active set first so you can restore it.
wp plugin list --status=active --field=name > /tmp/active-plugins.txt
wp plugin deactivate $(grep -v -E 'woocommerce$' /tmp/active-plugins.txt | tr '\n' ' ')
Test the button, then reactivate in halves until the click breaks again. Restore everything with wp plugin activate $(cat /tmp/active-plugins.txt | tr '\n' ' '). Page builders are frequent offenders here because they re-render product templates without the markup the add to cart script binds to.
Validate it works
Do not trust a reload. Prove the cart is stateful with a clean jar and two separate requests, and confirm the item count increments.
rm -f /tmp/verify.txt
curl -sS -o /dev/null -c /tmp/verify.txt --data 'product_id=123&quantity=1' 'https://example.com/?wc-ajax=add_to_cart'
curl -sS -o /dev/null -b /tmp/verify.txt -c /tmp/verify.txt --data 'product_id=123&quantity=1' 'https://example.com/?wc-ajax=add_to_cart'
grep -E 'woocommerce_items_in_cart|woocommerce_cart_hash' /tmp/verify.txt
example.com FALSE / TRUE 0 woocommerce_items_in_cart 2
example.com FALSE / TRUE 0 woocommerce_cart_hash 7ad1c93f0b62e845
woocommerce_items_in_cart reading 2 after two adds is the proof you want: the second request found the session written by the first. Then repeat by hand in a private browser window, add an item from a category archive rather than the product page, walk to the cart page, and refresh once. Both paths have to pass, because the archive uses the AJAX route and the product page may not.
Troubleshooting
It works while logged in as an administrator and fails for guests. Administrators bypass the page cache, so you are testing a different stack than your customers. Reproduce in a private window or with curl, then look at the cached guest HTML: a nonce baked into a page cached for hours is expired by the time anyone clicks, which returns a 403 on the AJAX route. Excluding the cart cookies is not enough here; the product and archive pages need a short enough cache lifetime that nonces stay valid, or nonce-free AJAX handling.
The item adds, then the cart is empty on the checkout page only. This is nearly always a hostname or scheme boundary crossed by a redirect. Compare the Domain column of the session cookie against the hostname in the address bar on both pages, and check for a plugin forcing HTTPS on checkout while the rest of the site answers on both schemes.
The mini cart count never changes although the cart is correct. The fragments response is being cached or blocked rather than the add itself. Request /?wc-ajax=get_refreshed_fragments directly and look for a cache HIT header, then clear the browser sessionStorage for the site — the fragments script keeps a copy there and will happily paint a stale one.
Carts empty at random, mostly at peak hours. Look at Redis eviction first, then at PHP-FPM saturation: a pool that queues requests past the browser timeout leaves the spinner turning forever while the add actually succeeded seconds later. Both are capacity problems wearing a WooCommerce costume.
A 500 appears only on the add to cart route. Read the log rather than guessing; the fatal is usually in a plugin hooked to woocommerce_add_to_cart_validation or woocommerce_add_cart_item_data. The WordPress error log walkthrough covers turning on the right logging without exposing it publicly.
When this is the wrong approach
If the cart fills correctly and the order fails at payment, this runbook does not apply — that is a gateway problem with its own diagnostic path. The same is true if the checkout page renders but no payment method appears.
If the storefront is headless or built on the Store API, the endpoint is /wp-json/wc/store/v1/cart/add-item rather than wc-ajax, and the cart is keyed by the Cart-Token header instead of a cookie. Most of the session reasoning still holds, but every command above needs re-pointing.
If the theme ships its own cart implementation, debug the theme. A custom AJAX cart that never touches WC_AJAX will not respond to any of these checks, and the fastest route is to read the theme’s own script.
And if the product is genuinely out of stock with backorders disabled, nothing here is broken. Change the stock rules or accept the refusal, but do not spend an outage window on it.
The references worth keeping open while you work are the WooCommerce settings documentation for the stock and cart behaviour options, and the WooCommerce developer portal for the AJAX and session APIs named above.
Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.