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 .
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
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.
WC_Cart, WC_Cart_Totals, and WC_Shipping work together to calculate the cost of shipping
WC_Cart, WC_Cart_Totals and WC_Discounts are used to calculate the discounts from applied coupons.
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.
The simplest of the tax scenarios, WC_Cart_Totals only needs to work with WC_Tax to calculate the taxes for items.
Fees follow a very similar tax calculation process, with one additional step being required, as the fees themselves must be calculated first.
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
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?
hint: you can use the navigation menu on the left to review the charts, while you answer the questions 😉
0 of 4 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 4 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Which of the following is not a class?
Which of the following are involved in shipping calculations (check all that apply)?
Which of the following are involved in calculating discounts? (check all that apply)
Which of the following have their own Tax calculation workflows? (check all that apply)