WC_Cart

Overview

 

A complex web of taxes, shipping, fees, and coupons makes the logic behind putting a final price on an order quite complex.

At the center of these calculations is the WC_Cart which sends data to a variety of sources to tally an order’s true cost .

Creation and Structure

WooCommerce Carts maintain data and calculations through an interplay between classes. It can feel a bit like eating spaghetti, at times. But each class does maintain a limited scope of concern.

WC_Cart: Serves as the central nexus.

 

WC_Cart_Session: Manages user data in the database.

 

WC_Cart_Fees: add fees (non-item charges)

 

WC_Cart_Totals: A series of functions for preparing the cart for checkout

 

Persistent cart is not a class, but a usermeta entry, bridging sessions

Loading the Cart

public function get_cart_from_session() {
	do_action( 'woocommerce_load_cart_from_session' );
	$this->cart->set_totals( WC()->session->get( 'cart_totals', null ) );
	$this->cart->set_applied_coupons( WC()->session->get( 'applied_coupons', array() ) );
	$this->cart->set_coupon_discount_totals( WC()->session->get( 'coupon_discount_totals', array() ) );
	$this->cart->set_coupon_discount_tax_totals( WC()->session->get( 'coupon_discount_tax_totals', array() ) );
	$this->cart->set_removed_cart_contents( WC()->session->get( 'removed_cart_contents', array() ) );

	$update_cart_session = false; // Flag to indicate the stored cart should be updated.
	$order_again         = false; // Flag to indicate whether this is a re-order.
	$cart                = WC()->session->get( 'cart', null );
	$merge_saved_cart    = (bool) get_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login', true );

	// Merge saved cart with current cart.
	if ( is_null( $cart ) || $merge_saved_cart ) {
		$saved_cart          = $this->get_saved_cart();
		$cart                = is_null( $cart ) ? array() : $cart;
		$cart                = array_merge( $saved_cart, $cart );
		$update_cart_session = true;

		delete_user_meta( get_current_user_id(), '_woocommerce_load_saved_cart_after_login' );
	}

When WC initializes, the cart is built from the session and reconciled with the database in WC_Cart_Session.

 

This occurs in the get_cart_from_session method.

Shipping Calculations

bubbles indicate filters and hooks, where custom code can apply changes to logic

WC_Cart, WC_Cart_Totals, and WC_Shipping work together to calculate the cost of shipping

Discount Calculations

Note the large number of hooks in the apply_coupon method

WC_Cart, WC_Cart_Totals and WC_Discounts are used to calculate the discounts from applied coupons.

Tax Calculations

To manage the potentially infinite combination of tax situations that a store owner may face, WooCommerce applies a specific tax calculation process for each of the default “cost” line types.

 

We’ll take a look atDon’t forget, if you ever need to zoom in on an image CTRL+Click will bring you in for a closer look each of these.

Item Taxes

The simplest of the tax scenarios, WC_Cart_Totals only needs to work with WC_Tax to calculate the taxes for items.

Fee Taxes

Fees follow a very similar tax calculation process, with one additional step being required, as the fees themselves must be calculated first.

Shipping Taxes

Shipping taxes are the most unique of the tax calculation scenarios, with the WC_Shipping Method calling WC_Tax, instead WC_Cart_Totals calling WC_Tax directly

Conclusion

There’s a lot of info in those diagrams. Remember, they’re here for you as a quick resource when you need to parse out the process

If you fully understand the way the WooCommerce Cart interacts with your customers and the site. Then your opportunities for customizing their purchasing experience become limitless.

But there’s one last stop before the Cart can become an order—the Checkout class.

Care to see what you’ve learned?

Knowledge Check

hint: you can use the navigation menu on the left to review the charts, while you answer the questions 😉

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