Learn how the WooCommerce cart really works — sessions, database storage, hooks, and key points to optimize your store's checkout experience.
Table of Contents
- The WooCommerce Cart Is More Than a Product List
- The Session: The Cart’s Invisible Core
- The WC_Cart Object: Where the Logic Lives
- WooCommerce Cart Hooks: Where Custom Code Plugs In
- The Full Flow: From “Add to Cart” to Checkout
- How Caching Affects the Cart
- Cart Fragments: The Magic Behind the Mini-Cart
- Persistent Cart: When the User Returns Days Later
- Customizing the Cart: What Works and What to Avoid
- Quick Diagnosis: Why the Cart Stops Working
- Frequently Asked Questions About the WooCommerce Cart
The WooCommerce Cart Is More Than a Product List
When a user clicks “Add to Cart” on a WooCommerce store, it looks like a simple action. Under the hood, however, there’s a surprisingly complex technical system at work. Understanding how the WooCommerce cart operates internally leads to better development decisions, faster troubleshooting, and fewer customizations that break the shopping experience. This guide covers the cart’s inner workings — from session storage and the data it manages to the hooks that let you modify its behavior and the failure points you’ll encounter most often.
The Session: The WooCommerce Cart’s Invisible Core
The WooCommerce cart relies on its own session system rather than PHP’s native session mechanism, though PHP sessions can serve as a fallback. By default, WooCommerce uses the WC_Session_Handler class, which stores cart data in the wp_woocommerce_sessions database table. Each visitor is identified by a cookie named woocommerce_cart_hash alongside woocommerce_session_{hash}.
The practical implications are significant. If the server or a caching plugin interferes with these cookies, the cart will appear empty after a redirect because the server can no longer match the session to the visitor. This is the most common issue in installations with aggressive caching that’s misconfigured: the page is served from cache, but the user’s session data doesn’t travel with it.
Data stored in the session includes:
- Product IDs and selected variations
- Quantities per line item
- Applied coupon data
- Calculated totals (subtotal, taxes, shipping)
- Custom metadata added by plugins or custom code
Every time the user modifies the cart, WooCommerce updates that database record and regenerates the cart hash. That hash is also used to detect changes and refresh the mini-cart via AJAX.
The WC_Cart Object: Where the Logic Lives
The central class that orchestrates everything is WC_Cart, accessible at runtime via WC()->cart. This object is initialized when the session loads and contains all the methods for adding, removing, or modifying items. From a development standpoint, the most relevant ones are:
add_to_cart(): adds a product and returns the unique cart item key.remove_cart_item(): removes an item by its key.get_cart(): returns the full array of cart items with all their data.calculate_totals(): recalculates prices, taxes, and shipping based on the current cart state.get_cart_contents_count(): returns the total number of units in the cart — useful for updating the header counter.
Understanding this structure is essential when working on customizations. Any logic that modifies prices or adds extra fields must hook into this flow correctly to avoid inconsistencies between what the user sees and what’s actually processed in the order.
WooCommerce Cart Hooks: Where Custom Code Plugs In
WooCommerce exposes a system of WordPress hooks — actions and filters — that let you modify cart behavior without touching core plugin files. This is the right way to customize: hook into the designated extension points rather than editing plugin files directly.
Key WooCommerce cart hooks include:
- woocommerce_add_to_cart: fires immediately after a product is added. Useful for tracking logic or notifications.
- woocommerce_cart_item_price: a filter that lets you modify the displayed price per line item. Commonly used for dynamic discounts or user-specific pricing.
- woocommerce_cart_item_subtotal: similar to the above, but applied to the line item subtotal.
- woocommerce_before_calculate_totals: runs before totals are calculated. This is the correct hook for modifying prices before WooCommerce processes them — for example, in volume pricing rules.
- woocommerce_cart_item_removed: lets you run logic when an item is removed, such as updating an external inventory record.
- woocommerce_cart_is_empty: a filter that controls whether WooCommerce treats the cart as empty — useful for conditional cart scenarios.

Using these hooks correctly is the difference between a robust customization and one that breaks on the next WooCommerce update.
The Full Flow: From “Add to Cart” to Checkout
Here’s a step-by-step look at what happens technically from the moment the user clicks the button to when they reach the payment page:
- AJAX request or page reload: depending on configuration and theme, adding to the WooCommerce cart can work via AJAX (no page reload) or with a direct redirect to the cart page. WooCommerce calls the endpoint
/?wc-ajax=add_to_cart. - Product validation: WooCommerce checks that the product exists, has available stock, and that the selected variations are valid.
- Cart item key generation: a unique hash (the cart item key) is generated to identify that specific combination of product, variation, and metadata. Two identical items with the same metadata share a key and their quantities are summed; if the metadata differs, separate line items are created.
- Session write: the cart array is updated in
wp_woocommerce_sessionswith the new data. - Browser response: in AJAX mode, WooCommerce returns the updated mini-cart fragment and item count. The theme receives this fragment and replaces it in the DOM.
- Total calculation: when the user visits the cart page,
calculate_totals()processes taxes based on the customer’s geolocation, applies active coupons, queries shipping rules, and updates all subtotals. - Proceed to checkout: when moving to checkout, WooCommerce transfers the active cart data to a temporary order object, from which payment and order confirmation are handled.
How Caching Affects the WooCommerce Cart
This is perhaps where the most projects run into trouble. The cart and checkout pages must never be cached. WooCommerce adds the cookie woocommerce_items_in_cart when products are in the cart, precisely so that caching plugins can automatically exclude these pages.
However, each caching plugin handles this differently. WP Rocket, for instance, has native WooCommerce configuration that automatically excludes the cart, checkout, and My Account pages. LiteSpeed Cache and W3 Total Cache require more careful manual configuration. The typical problem: the server serves a cached version of the cart page that doesn’t reflect the user’s actual items, causing confusion and cart abandonment.
Object caching plugins (Redis, Memcached) can also interfere with session storage if they’re not configured to distinguish between page cache and user data cache. A developer who doesn’t understand this distinction can spend hours hunting a bug that’s actually a caching issue.
Cart Fragments: The Magic Behind the Mini-Cart
WooCommerce uses a system called “cart fragments” to update the mini-cart (that small icon with a counter in the header) without reloading the page. Here’s how it works: on every page load, WooCommerce makes an AJAX request to /?wc-ajax=get_refreshed_fragments to retrieve the updated mini-cart HTML.
While this improves the user experience, it can create performance issues on high-traffic sites: every visit triggers an extra AJAX request, increasing server load. Solutions include disabling fragments on pages where no cart is visible, or deferring the request until the user interacts with the site.
The fragments system is also extensible: you can add your own elements to the fragments array using the woocommerce_add_to_cart_fragments filter, which lets you update any DOM element — not just the mini-cart — whenever the WooCommerce cart changes.
Persistent Cart: When the User Returns Days Later
WooCommerce includes a persistent cart feature for logged-in users. When a registered user adds products to the cart and closes their browser, those products are still there when they return. This data is stored in user meta (wp_usermeta), not in the temporary sessions table.
For anonymous users, the cart persists as long as the session cookie remains valid. By default, the session expires after 48 hours of inactivity — a value you can configure using the wc_session_expiring and wc_session_expiration filters.
The persistent cart for registered users can be enabled or disabled under WooCommerce > Settings > Accounts & Privacy. For B2B stores where customers build complex orders over several days, keeping it active is essential to avoid frustrating repeat buyers.
Customizing the WooCommerce Cart: What Works and What to Avoid
A common question on WooCommerce projects is how far to push cart customization — and how to do it correctly. The main approaches are:
Hook-based customization (recommended)
As described above, WooCommerce hooks are the right method. They let you add columns to the cart table, display custom metadata per item (such as engravings, dedications, or product configurations), modify prices dynamically, or add contextual messages based on what’s in the cart.
Template customization
WooCommerce allows you to override its templates by copying files from woocommerce/templates/ to your-theme/woocommerce/. The cart template is cart/cart.php. This approach gives you full control over the HTML, but it comes with a maintenance cost: every time WooCommerce updates that template, you need to review whether those changes should be merged into your custom copy.
Page builders (proceed with caution)
Some page builders offer WooCommerce-specific cart blocks. The native WooCommerce block in the block editor is the most stable option. Third-party builders can create problematic dependencies or conflict with advanced cart plugins. This path requires thorough testing before going live.
Quick Diagnosis: Why the WooCommerce Cart Stops Working
When the cart fails on a WooCommerce project, the most common symptoms and their root causes tend to follow a predictable pattern:
- Cart always appears empty: cookie issue (incorrect HTTPS, misconfigured domain) or cache serving pages without a session.
- Cart doesn’t update when adding products: JavaScript conflict between the theme and WooCommerce, or an AJAX request blocked by a security plugin.
- Totals don’t add up: a plugin modifying prices via
woocommerce_before_calculate_totalswith faulty logic, or a conflict between pricing and coupon plugins. - Mini-cart doesn’t update: the theme doesn’t include the
woocommerce_cart_link_fragmenthook correctly, or a plugin removes the fragment request. - Cart data lost between pages: sessions not persisted correctly — sometimes caused by load balancers without sticky sessions in shared hosting environments with multiple nodes.
Frequently Asked Questions About the WooCommerce Cart
Does the WooCommerce cart store data in cookies or in the database?
Both. The cookie identifies the visitor, but the actual cart data is stored in the wp_woocommerce_sessions database table. The cookie acts as the key; the database holds the contents.
Can a user have multiple simultaneous carts?
Not natively. WooCommerce manages a single active cart per session. Use cases like multiple carts or wish lists require dedicated plugins or custom development.
What happens to the cart if the user doesn’t complete the purchase?
The session expires after 48 hours of inactivity (configurable). For registered users, the cart is saved to their profile. Abandoned cart recovery plugins capture the user’s email during checkout and send reminders if the purchase isn’t completed before the session expires.
Does the number of products in the cart affect performance?
With dozens of distinct items, yes — especially when calculating totals with complex tax rules or elaborate shipping configurations. In B2B catalogs with 50–100 line item orders, optimizing the calculate_totals() process may be necessary.
How does the WooCommerce cart handle variable products?
Each variation combination (size, color, etc.) generates a different cart item key. If the user adds the same variation twice, WooCommerce increments the quantity rather than creating a new line. If the user adds two different variations of the same product, they appear as separate line items.
If you’re evaluating how to structure or improve the cart on your WooCommerce store, my services page outlines the types of projects I work on and the approach I apply in each case.
My Take as a WordPress Developer
The most common thing I find in inherited projects is that nobody documented how sessions were configured or which hooks modify cart prices. When something breaks months after launch, reconstructing that logic from scratch takes far more time than proper documentation would have required up front. The WooCommerce cart looks like a simple component, but it’s where caching, sessions, JavaScript, and business logic all converge at once. Understanding how that system works internally isn’t a minor technical detail — it’s what allows you to keep a store running reliably over the long term without resorting to trial and error every time something stops working.
Need help with your project? I work with businesses and agencies on WordPress, WooCommerce, AI and integrations. Get in touch and we can discuss it.