WooCommerce Hooks for Cart Data (Total, Items, and Customer Info) from $cart Object

WooCommerce Tutorial

WooCommerce Hooks for Cart Data: Retrieving Cart Data (Total, Items, and Customer Info) from the $cart Object in WooCommerce – WooCommerce’s shopping cart system allows developers to interact with it programmatically using the $cart object.

This object provides access to cart data, including totals, items, shipping details, applied discounts, and customer billing and shipping information.

Developers can use WooCommerce’s methods to access all aspects of a customer’s cart and create custom checkout flows, reports, or integrations with third-party systems.

WooCommerce Hooks for Cart Data (Total, Items, and Customer Info)

Previously we have covered WooCommerce Hooks for Order Data to retrieve order data and WooCommerce Hooks for Product Data to get single product data.

TL;DR

Hide
  • WooCommerce provides several conditionals to verify if the cart is empty, if it needs payment or shipping, and more.
  • WooCommerce provides several methods to retrieve the financial data of the cart, including the subtotal, total taxes, shipping, discounts, and the overall total.
  • To retrieve details about the individual products added to the cart, you can loop through the items in the cart.
  • WooCommerce stores the customer's billing and shipping details in the cart, which you can retrieve using the following methods.
  • Beyond the core data like items and totals, WooCommerce allows you to access several other pieces of cart-related information that can help you build more customized cart experiences.
  • By using these functions, it is possible to create custom checkout experiences, summaries and even integrate cart data into external systems.
  • The WooCommerce cart object is a versatile and useful tool for handling and managing shopping cart data.

In this article, we’ll cover how to access cart-related data from the $cart object and break down the different methods you can use to retrieve totals, cart items, customer details, and more.

Here’s the complete WooCommerce hooks for cart data:

// Access $cart object
$cart = WC()->cart;

// $cart conditionals (if)
WC()->cart->is_empty()
WC()->cart->needs_payment()
WC()->cart->show_shipping()
WC()->cart->needs_shipping()
WC()->cart->needs_shipping_address()
WC()->cart->display_prices_including_tax()
 
// Get $cart totals
WC()->cart->get_cart_contents_count();
WC()->cart->get_cart_subtotal();
WC()->cart->subtotal_ex_tax;
WC()->cart->subtotal;
WC()->cart->get_displayed_subtotal();
WC()->cart->get_taxes_total();
WC()->cart->get_shipping_total();
WC()->cart->get_coupons();
WC()->cart->get_coupon_discount_amount( 'coupon_code' );
WC()->cart->get_fees();
WC()->cart->get_discount_total();
WC()->cart->get_total(); // formatted string $123.99 
WC()->cart->get_total( 'edit' ); // unformatted float 123.99
WC()->cart->total;
WC()->cart->get_tax_totals();
WC()->cart->get_cart_contents_tax();
WC()->cart->get_fee_tax();
WC()->cart->get_discount_tax();
WC()->cart->get_shipping_total();
WC()->cart->get_shipping_taxes();
  
// Loop over $cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
   $product = $cart_item['data'];
   $product_id = $cart_item['product_id'];
   $variation_id = $cart_item['variation_id'];
   $quantity = $cart_item['quantity'];
   $price = WC()->cart->get_product_price( $product );
   $subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] );
   $link = $product->get_permalink( $cart_item );
   // Anything related to $product, check $product tutorial
   $attributes = $product->get_attributes();
   $whatever_attribute = $product->get_attribute( 'whatever' );
   $whatever_attribute_tax = $product->get_attribute( 'pa_whatever' );
   $any_attribute = $cart_item['variation']['attribute_whatever'];
   $meta = wc_get_formatted_cart_item_data( $cart_item );
}
 
// Get $cart customer billing / shipping
WC()->cart->get_customer()->get_billing_first_name();
WC()->cart->get_customer()->get_billing_last_name();
WC()->cart->get_customer()->get_billing_company();
WC()->cart->get_customer()->get_billing_email();
WC()->cart->get_customer()->get_billing_phone();
WC()->cart->get_customer()->get_billing_country();
WC()->cart->get_customer()->get_billing_state();
WC()->cart->get_customer()->get_billing_postcode();
WC()->cart->get_customer()->get_billing_city();
WC()->cart->get_customer()->get_billing_address();
WC()->cart->get_customer()->get_billing_address_2();
WC()->cart->get_customer()->get_shipping_first_name();
WC()->cart->get_customer()->get_shipping_last_name();
WC()->cart->get_customer()->get_shipping_company();
WC()->cart->get_customer()->get_shipping_country();
WC()->cart->get_customer()->get_shipping_state();
WC()->cart->get_customer()->get_shipping_postcode();
WC()->cart->get_customer()->get_shipping_city();
WC()->cart->get_customer()->get_shipping_address();
WC()->cart->get_customer()->get_shipping_address_2();
 
// Other stuff
WC()->cart->get_cross_sells();
WC()->cart->get_cart_item_tax_classes_for_shipping();
WC()->cart->get_cart_hash();
WC()->cart->get_customer();

Accessing Cart Conditionals and States

The first step when working with WooCommerce’s cart is often checking its current state.

WooCommerce provides several conditionals to verify if the cart is empty, if it needs payment or shipping, and more.

These conditionals are useful when building custom checkout logic or displaying relevant notices to users.

WC()->cart->is_empty(); // Check if the cart is empty
WC()->cart->needs_payment(); // Check if the cart needs a payment method
WC()->cart->show_shipping(); // Check if shipping is shown for the cart
WC()->cart->needs_shipping(); // Check if the cart needs shipping
WC()->cart->needs_shipping_address(); // Check if the cart needs a shipping address
WC()->cart->display_prices_including_tax(); // Check if prices should be displayed with tax included
  • is_empty() helps you determine if there are any items in the cart, which is often used to prevent checkout if the cart is empty.
  • needs_payment() and needs_shipping() can be used to verify if the cart requires payment or shipping services.

Retrieving Cart Totals

WooCommerce provides several methods to retrieve the financial data of the cart, including the subtotal, total taxes, shipping, discounts, and the overall total.

WC()->cart->get_cart_contents_count(); // Get the number of items in the cart
WC()->cart->get_cart_subtotal(); // Get the cart subtotal
WC()->cart->subtotal_ex_tax; // Get subtotal excluding tax
WC()->cart->subtotal; // Get the subtotal including tax
WC()->cart->get_displayed_subtotal(); // Get the subtotal displayed to customers
WC()->cart->get_taxes_total(); // Get total tax amount
WC()->cart->get_shipping_total(); // Get the total shipping cost
WC()->cart->get_coupons(); // Get the coupons applied to the cart
WC()->cart->get_coupon_discount_amount('coupon_code'); // Get the discount amount for a specific coupon
WC()->cart->get_fees(); // Get any additional fees applied to the cart
WC()->cart->get_discount_total(); // Get the total discount applied to the cart
WC()->cart->get_total(); // Get the formatted total string (e.g., "$123.99")
WC()->cart->get_total('edit'); // Get the total as an unformatted float value (e.g., 123.99)
WC()->cart->get_tax_totals(); // Get the tax totals for the cart
WC()->cart->get_cart_contents_tax(); // Get the total tax on cart items
WC()->cart->get_fee_tax(); // Get the tax amount applied to fees
WC()->cart->get_discount_tax(); // Get the tax discount applied to the cart
WC()->cart->get_shipping_taxes(); // Get the tax applied to the shipping
  • get_cart_subtotal() returns the cart subtotal before shipping and discounts are applied.
  • get_total() returns the final total of the cart, which is a formatted string ready for display.
  • get_taxes_total() and get_shipping_total() allow you to access the total tax and shipping costs, respectively.

These methods provide the financial breakdown of the cart, which is useful for creating custom order summaries, checkout pages, or providing detailed receipts.


Looping Over Cart Items

To retrieve details about the individual products added to the cart, you can loop through the items in the cart.

Each item includes information like product ID, variation ID, quantity, price, and attributes.

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
   $product = $cart_item['data']; // Get the WC_Product object for the item
   $product_id = $cart_item['product_id']; // Get the product ID
   $variation_id = $cart_item['variation_id']; // Get the variation ID (if any)
   $quantity = $cart_item['quantity']; // Get the quantity of the product
   $price = WC()->cart->get_product_price( $product ); // Get the price of the product
   $subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] ); // Get the subtotal for the item
   $link = $product->get_permalink( $cart_item ); // Get the product permalink
   $attributes = $product->get_attributes(); // Get the product attributes
   $whatever_attribute = $product->get_attribute( 'whatever' ); // Get a specific attribute
   $whatever_attribute_tax = $product->get_attribute( 'pa_whatever' ); // Get a specific tax attribute
   $any_attribute = $cart_item['variation']['attribute_whatever']; // Get variation-specific attribute
   $meta = wc_get_formatted_cart_item_data( $cart_item ); // Get formatted item meta data
}
  • get_cart() retrieves all the items currently in the cart.
  • get_product_price() returns the price of the product as formatted HTML, while get_product_subtotal() gives you the subtotal (quantity x price) for that item.
  • get_permalink() provides the product URL, which is useful for creating clickable product links in cart displays or checkout pages.

By looping through the items in the cart, you can access all the relevant details for each product, such as its name, price, quantity, and attributes, which is essential for displaying cart contents to customers.


Accessing Customer Billing and Shipping Information

WooCommerce stores the customer’s billing and shipping details in the cart, which you can retrieve using the following methods.

This is particularly useful when preparing the checkout page or integrating with third-party shipping providers.

WC()->cart->get_customer()->get_billing_first_name(); // Get billing first name
WC()->cart->get_customer()->get_billing_last_name(); // Get billing last name
WC()->cart->get_customer()->get_billing_company(); // Get billing company
WC()->cart->get_customer()->get_billing_email(); // Get billing email
WC()->cart->get_customer()->get_billing_phone(); // Get billing phone number
WC()->cart->get_customer()->get_billing_country(); // Get billing country
WC()->cart->get_customer()->get_billing_state(); // Get billing state
WC()->cart->get_customer()->get_billing_postcode(); // Get billing postcode
WC()->cart->get_customer()->get_billing_city(); // Get billing city
WC()->cart->get_customer()->get_billing_address(); // Get billing address line 1
WC()->cart->get_customer()->get_billing_address_2(); // Get billing address line 2
WC()->cart->get_customer()->get_shipping_first_name(); // Get shipping first name
WC()->cart->get_customer()->get_shipping_last_name(); // Get shipping last name
WC()->cart->get_customer()->get_shipping_company(); // Get shipping company
WC()->cart->get_customer()->get_shipping_country(); // Get shipping country
WC()->cart->get_customer()->get_shipping_state(); // Get shipping state
WC()->cart->get_customer()->get_shipping_postcode(); // Get shipping postcode
WC()->cart->get_customer()->get_shipping_city(); // Get shipping city
WC()->cart->get_customer()->get_shipping_address(); // Get shipping address line 1
WC()->cart->get_customer()->get_shipping_address_2(); // Get shipping address line 2
  • get_billing_first_name() and similar methods retrieve the customer’s billing information, such as their name, address, and email.
  • get_shipping_first_name() and related methods retrieve the customer’s shipping details.

These methods allow you to fetch customer billing and shipping addresses, which can be displayed on the checkout page or passed to third-party systems for order processing.


Additional Cart-Related Data

Beyond the core data like items and totals, WooCommerce allows you to access several other pieces of cart-related information that can help you build more customized cart experiences.

WC()->cart->get_cross_sells(); // Get cross-sell products for the cart
WC()->cart->get_cart_item_tax_classes_for_shipping(); // Get tax classes for shipping
WC()->cart->get_cart_hash(); // Get the cart hash, useful for cart uniqueness
WC()->cart->get_customer(); // Get the customer object associated with the cart
  • get_cross_sells() returns an array of product IDs that are cross-sell items for the current cart, allowing you to display related product suggestions.
  • get_cart_hash() generates a hash of the cart contents, which is often used to detect changes in the cart during an active session.

Wrapping Up

The WooCommerce cart object is a versatile and useful tool for handling and managing shopping cart data.

It provides various functions to meet different requirements, such as accessing cart totals, items, and displaying customer-related information.

By using these functions, it is possible to create custom checkout experiences, summaries and even integrate cart data into external systems.

Related Posts