WooCommerce Session Cart & Checkout Primer

Overview

The auto-magic nature of going from the catalog to an order keeps most store owners from thinking too hard about sessions, carts, checkout forms, etc.

…until they decide to create a unique checkout experience for their customers—then advanced WooCommerce developers need to get into the code to fine-tune the experience.

Let’s take a look behind the curtain…

If you prefer to bypass the lesson, and jump straight to the quiz: click here

Session

The WooCommerce Session remembers the customers’ selections, information and data by linking a database entry to a cookie in the user’s browser

Cart

The cart, which is stored in the session, stores selections for a future order and calculates the order’s pricing.

Checkout

The WC_Checkout singleton object collects additional information and builds an order.

WC_Session Classes

WooCommerce’s core provides an abstract WC_Session class, which can be used to create a custom handler, and a default session handler, which can be overriddenThis allows you to consider unique implementations of the session handler–e.g. Perhaps instead of using a cookie on the customer’s browser it would be better to use the fingerprint or IP address

Two additional classes directly pertain to specific data sets with in the WC session:

  • WC_Customer_Data_Store_SessionThis class manages data associated with the customer, regardless of whether or not the customer has a WP User account
  • WC_Cart_SessionThis class manages storage and retrieval of data specific to the user’s cart

Cookies

A sessionIn it’s default implementation “connects” with the user’s browser using the wp_woocommerce_session cookie

Two additional cookies are set, which are primarily used by the front-end’s refresh_cart_fragmentsWhich is much maligned, for performance reasons. functionality:

  • woocommerce_cart_hash
  • woocommerce_items_in_cart

Sessions in the Database

WC_Sessions are stored in the woocommerce_sessions table in the database. The fields are fairly straightforward:

Field Description
session_id Unique ID int
session_key customer’s ID
session_value Serialized array of all the data associated with the customer’s session
session_exipry timestamp of session’s expiration

WC_Cart Classes

Conceptually simple, but practically complex, the WC_Cart classes in WooCommerce do a lot of heavy lifting. We’ll dive into the details later.

The WC_Checkout Class

A cursory review of the public methods in WC_Checkout will show that we have a bit of a dog’s breakfast of functionalityFrom Urban Dictionary: “dog’s breakfast,” which has been British slang for “a complete mess” since at least the 1930s… a failed culinary effort… fit only for consumption by the mouth of last resort, Fido..

Think of this as the “manager” class for the checkout. It identifies and collects the information needed to build an order–and then builds it.

Conclusion

We’ll take an even deeper dive into each of these components in subsequent topics, then take a look at a couple uses cases for creating a unique browsing and checkout experience for your customers.

Let’s do a quick knowledge check, before moving on to the next topics

Knowledge Check

References

×

Keyboard shortcuts

CTRL+Shift+F Search slideshow
F Fullscreen view
CTRL+Click Zoom in
Esc Topic overview
Right arrow,
Down arrow
Next slide
Left arrow,
Up arrow
Previous slide

Color codes

Tooltip
Hover over text more additional info
Link to an external resource
Link to an internal slide
If buttons aren't working, click in the screen to "focus" your browser
RSS
LinkedIn
Share