WooCommerce mini cart is cached and already has a product in it
Understand the problem
When a visitor goes to your site for the first time, the mini-cart seems to be cached and already has an item (or items) in it. Visiting the Cart page itself shows there are no items in the cart, but the widget does not update, or the mini-cart may correct itself after a short delay.
This can happen for two different reasons:
- The WooCommerce Cart Fragments request was cached.
This should be the case if the page source shows the correct cart but it looks incorrect when the page loads, use Solution 1. - A page was cached while a visitor had products in their cart.
This is the case If the incorrect cart is already present in the page source, use Solution 2. - In some cases, both solutions may be needed.
Solution 1: Stop caching the Cart Fragments request
WooCommerce uses Cart Fragments to update the mini-cart after the page loads. In some cases, WP Rocket’s optimization of this request can cause incorrect cart information to appear.
Install and activate the following helper plugin on your site to deactivate that optimization:
📥 Download (.zip): WP Rocket | Deactivate WooCommerce Refresh Cart Fragments Cache
Developers: you can find the code for this plugin on GitHub.
Solution 2: Prevent users with active carts from creating public cache files
Some themes add the visitor's cart directly to the page HTML. If that user visits a page that has not been cached yet, their cart information could be included in the cache file and shown to other visitors.
Install and activate the following helper plugin to prevent that:
📥 Download (.zip): WP Rocket | Prevent Cache Generation for Active Carts
Developers: you can find the code for this plugin on GitHub.
If this solution does not work, the issue could be a different one, related to the way the cart widget on your site is coded. Please see this guide for more details.