Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

WooCommerce "Sorry, your session has expired. Return to shop" at the classic checkout

Technical article
Hosting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

The WooCommerce classic checkout shows "Sorry, your session has expired. Return to shop" when a customer presses Place order, the form's security token fails its check, and the cart on the server is empty. Either the shop's cookie never reached the server, or the session behind it has ended. The fix is to find which of these happened. WooCommerce's documentation also says Cart, My Account and Checkout must stay out of every cache.

What the error means

In the WooCommerce 11.3.1 source, process_checkout() reads the token sent with the form, from woocommerce-process-checkout-nonce or _wpnonce, and checks it with wp_verify_nonce() for the action woocommerce-process_checkout. If the token is empty or fails, the code looks at the cart. An empty cart throws the message Sorry, your session has expired. <a href="%s" class="wc-backward">Return to shop</a>, where the link goes to the shop page. A cart that still holds items sets the totals to refresh and throws a different message, one that ends with "please try again." Both the AJAX request and the ordinary form post reach this same function: class-wc-ajax.php and class-wc-form-handler.php each call WC()->checkout()->process_checkout();.

The message is therefore two facts in one: the token failed, and the server could not find the customer's cart. A token that fails while the cart still holds items gives the other message.

WordPress documents that a nonce has a lifetime of one day by default, and that the real lifetime varies between 12 and 24 hours. WooCommerce's session handler also changes how tokens work for visitors who are not signed in: for a nonce action starting with woocommerce, it uses the customer ID of the session in place of the user ID. A token is thus tied to that visitor's session.

WooCommerce documents the cookie wp_woocommerce_session_ as a code "so that it knows where to find the cart data in the database for each customer", with a duration of 2 days. In the source, a guest session lasts 2 days and a signed-in customer's lasts one week, with a hard cap of 30 days.

Likely causes

Cause What you see How to confirm
The session cookie is blocked or never returned to the server Message on every attempt for that customer, cart looks empty No wp_woocommerce_session_ cookie in the browser after adding an item
Cart, Checkout or My Account is not excluded from the page cache The documentation says these pages must stay dynamic The cache's exclusion list lacks those pages
The customer left the checkout open longer than the session lasts Message after a long pause, then works after adding items again A wait longer than the session: 2 days for a guest, one week for a signed-in customer. A shorter wait gives the other message ("please try again.")
Code shortens session or token life Message after a shorter pause than the defaults A search finds wc_session_expiration or nonce_life in plugin code
A database cache is not told to leave session rows alone The documentation suggests excluding _wc_session_ The database cache's exclusion list lacks _wc_session_

Prove the cause before changing anything

Every step in this section only reads.

  1. In a private browser window, add an item to a test cart on the shop (the cart is not ordered and holds nothing of a customer's), then open the browser's cookie list for the shop's address. A good result shows a cookie whose name starts with wp_woocommerce_session_. If it is absent, the cookie is not reaching the browser or is blocked in it. For blocked cookies in general, see WordPress login "Cookies are blocked or not supported by your browser": the test cookie, COOKIE_DOMAIN and the Secure flag.
  2. In the settings of the caching plugin, host cache or CDN, read the list of excluded pages and excluded cookies. WooCommerce's documentation says to exclude Cart, My Account and Checkout, which "need to stay dynamic since they display information specific to the current customer and their cart." A bad result is a list without them.
  3. Search the site's code for shortened lifetimes. Run it from the WordPress folder.
grep -rn "wc_session_expiration\|wc_session_expiring\|nonce_life" wp-content/plugins wp-content/mu-plugins wp-content/themes

The grep manual page defines -r as reading all files under each directory recursively, -n as prefixing each output line with its line number, and \| as alternation between patterns. A hit shows code that may change the lifetime; read what it does. The source's default for guests is 2 days.

  1. Ask the customer how long the checkout page sat open. A token expires after 12 to 24 hours, but this message also needs an empty cart, and a guest's cart lasts 2 days (a signed-in customer's one week). A wait of one to two days therefore gives the other message ("please try again."); a wait longer than the session gives this one, with no fault on the site.

Fix

Pages that are not excluded from the cache

The documentation's instruction: if a caching system is in use, exclude the Cart, My Account and Checkout pages through its settings; if it offers database caching, exclude _wc_session_; the cookies woocommerce_cart_hash, woocommerce_items_in_cart, wp_woocommerce_session_, woocommerce_recently_viewed and store_notice[notice id] can be excluded from caching.

First save the current state: write down the cache's present list of excluded pages, cookies and database-cache exclusions, and the time.

Risk: The next change alters what the cache serves to every visitor. The three excluded pages are built fresh for each request, so the server does more work on them. Purging stored copies makes the next visitors wait while pages rebuild.

In the cache's settings, add Cart, My Account and Checkout to the excluded pages. Add _wc_session_ to the database-cache exclusions and the cookies above to the excluded cookies, where the cache offers those fields. Then use the cache's own purge for those three pages so that no old copy remains.

To undo it:

Risk: Putting the lists back makes the cache serve Cart, My Account and Checkout again, and purging makes visitors wait while pages rebuild.

In the cache's settings, put back exactly the lists of excluded pages, cookies and database-cache exclusions that you wrote down, then use the cache's own purge once more.

Then add an item to a test cart and check the cookie as in step 1.

The documentation gives a Varnish rule that passes requests for /cart, /my-account and /checkout straight to the backend. Where Varnish is in use, give that rule to whoever manages it.

The cookie is blocked or the customer waited too long

A cookie that the visitor's own browser blocks, or a wait longer than the session, is not a fault on the site. For a cache that is not excluding the pages, see the first fix above. For a blocked or a waited-out session, the customer follows the Return to shop link, adds the items again and completes the order within the same visit. Tell customers who work slowly to finish in one sitting.

Code shortens the lifetimes

If the search found wc_session_expiration, wc_session_expiring or nonce_life, the plugin or theme that sets them decides the lifetime. The source logs a warning when a session would last longer than 30 days, since a longer session grows the session table. What to change, and how, is set out in that plugin's or theme's own documentation.

Example (illustrative): the shop at shop.example.com run by Example Pty Ltd receives a complaint from user1@example.com, who left the checkout open from Friday evening to Monday morning, about 3 days, then pressed Place order. A guest session lasts 2 days, which is 2 × 86,400 = 172,800 seconds, and the token's life is at most 86,400 seconds, so by Monday both had ended. The cart was empty and the token failed, so the shop printed the expiry message. Had the customer pressed Place order after one and a half days, the token would have failed while the cart still held items, and the shop would have printed the "please try again." message. A second customer sees the same message within seconds of pressing Place order. The cache plugin's exclusion list does not contain Checkout, so the administrator adds it, purges the page and the second customer's order goes through.

Confirm the fix

  1. In a private window, add an item to a test cart and check that the cookie wp_woocommerce_session_ is present.
  2. Request the checkout page twice, and confirm the cache is not answering from a stored copy, using the status header your cache adds, if it adds one.
  3. Place an order on a test product the way your shop normally tests orders, and check that the confirmation page appears with no expiry message.

Risk: A real order creates an order record, takes stock and sends emails. Use a test product, or cancel the order afterwards in WooCommerce.

What to watch afterwards

  • Each time you change a caching plugin, CDN rule or host cache, read its exclusion list again.
  • Watch for the WooCommerce log entry from wc_session_handler about sessions longer than 30 days.
  • If customers report the other message, the one that ends "please try again.", the cart still had items and only the token failed. The same checks apply, because the cookie and the cache decide whether the token and the cart reach the server.

Risks and limits

  • These commands have not been run or lab-tested.
  • The message and its two branches come from the WooCommerce 11.3.1 classic checkout code.
  • Session and token lifetimes depend on filters a site may set; the figures here are the defaults in the cited source.
  • Cache settings differ by plugin and host, and the documentation says it cannot cover every option.

Sources