The WC Session Life Cycle

Overview

WooCommerce faces a challenge not present on every WordPress site:

It must identify and remember each visitor that interacts with the store, in order to remember their preferences and build a sales order– regardless of whether the user is registered you could, hypothetically, require every user to register with you WP site
But that would create a lot a “friction” to the purchase
with the site
.

And WooCommerce needs to do this while maintaining the security necessary for eCommerce sites.

Let’s take a look at how WooCommerce meets these requirements using the WC_Session_Handler throughout each run.

Initialization

WC()->init()

wc_load_cart()

WC()->initialize_session()

Cycle per PHP run

While the WC_Session depends on the cookiewp_woocommerce_session to identify the user, no significant information is stored in that cookie. Instead, the Session assesses if there is new informationaka is the session “dirty” to save, and then updates the database entry accordingly.

The Session’s Data

Multiple classes use the Session to save customer information, for a seamless of a checkout experience. WC_AJAX often serves as the entry point for processing and forwarding customer information.

Regulations

Currently the 800lb gorilla governing internet privacy is the European Union’s General Data Protection GuidelinesGDPR, for short. If your site is used by citizens of the EU–regardless of where you or they are in the world–you need to comply with these regulations.

Site Owners must:

  • Tell the user who you are, why you collect the data, for how long, and who receives it.
  • Get a clear consent [when required] before collecting any data.
  • Let users access their data, and take it with them.
  • Let users delete their data.
  • Let users know if data breaches occur.

These regulations are technology agnostic, so creative implementations of Session Handlers will not circumvent these requirements

Customizing the Session Handler

add_filter('woocommerce_session_handler', function($handler){
	return "My_Awesome_Session_Handler";
});

class My_Awesome_Session_Handler extends WC_Session{
	//Do creative and awesome stuff to manage your user sessions here
}

While the default WC_Session_Handler works fine, developers may decide to craft unique customer checkout experiences using custom session handlers. Fortunately, WooCommerce makes this easy with a single filter hook.

Conclusion

Now that you understand how WooCommerce remembers and stores data for each of the site’s customers, you’ll need to consider how WooCommerce combines all of these data points to start building a sale.

To understand that, we need to look at the WC_Cart and its family of classes.

Before moving ahead, take a quick quiz to review.

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