A guide to the WC_Cart class in WooCommerce

In this article, we will explore the WooCommerce WC_Cart class, focusing on the key methods and properties. We’ll learn how to retrieve cart items, extract cart total values, and access customer details. By the end of this guide, you will have a clear understanding of how to effectively use the WC_Cart class in your WooCommerce store.

How is the WooCommerce cart stored

In WooCommerce, the cart data is typically stored in the user’s session. When a user adds items to their cart, WooCommerce uses the WordPress session handler to store this information. This approach ensures that the cart data is unique to each visitor and persists across page loads. For logged-in users, the cart contents can also be optionally saved to the database, allowing the cart data to be retained across different sessions. This can be especially useful if you need to implement a feature to view customer cart details directly from the WooCommerce admin dashboard.

wc_cart class

How to get the current cart object in WooCommerce

To access the current cart in WooCommerce, use the following line of code:

$cart = WC()->cart;

This code snippet fetches an instance of the WC_Cart class. The WC() function is a global instance of the WooCommerce class, and cart is a property of this instance. By accessing WC()->cart, you’re directly retrieving the current user’s cart instance. This instance contains all the necessary data and methods related to the user’s shopping cart, such as items added, quantities, prices, and other cart-related details.

How to check if the current cart has items

In WooCommerce, determining whether the current cart contains items can be achieved by using the is_empty() method from the WC_Cart class. Here’s how you do it:

if ( WC()->cart->is_empty() ) {
  // Actions to perform if the cart is empty
} else {
  // Actions to perform if the cart has items
}

The is_empty() method returns a boolean value. It returns true if the cart is empty (i.e., there are no items in the cart) and false if the cart contains one or more items. This method is particularly useful when you need to display different information or options to users based on whether they have items in their cart. For instance, you might want to prompt users to continue shopping if their cart is empty or proceed to checkout if it’s not.

Unlock the Full Potential of Your WooCommerce Customer Data

Getting the number of items in the cart

To determine the total number of items in a WooCommerce cart, you can use the get_cart_contents_count() method from the WC_Cart class. It’s important to note that this method counts the total quantity of all items in the cart, not the number of unique products. For example, if a customer adds 5 units of the same product, get_cart_contents_count() will return 5.

Here’s how to use this method to get the cart contents count:

$total_items = WC()->cart->get_cart_contents_count();

How to get cart items

To access the items in a WooCommerce cart, you can use the get_cart() method of the WC_Cart instance. This method returns an array of cart item data, where each item is represented as an associative array with details like product ID, product data, quantity, subtotal, and variation ID (if applicable).

Here’s a code snippet that demonstrates how to iterate over these items and extract their details:

$items = WC()->cart->get_cart();

foreach ($items as $item) {
  $product_id = $item['product_id']; // access the product ID

  $product = $item['data']; // access the product object
  echo $product->get_title();

  echo $item['quantity']; // quantity
  // total and subtotal
  echo $item['line_subtotal'];
  echo $item['line_total'];

  if (!empty($item['variation_id'])) {
    echo $item['variation_id'];
  }
}

In this snippet, $items contains the array of all items in the cart. Each $item in the $items array has several properties, including:

  • product_id – the ID of the product
  • data – the product object, it is an instance of the WC_Product class
  • quantity – shows how many of those items are in the cart
  • line_subtotal and line_total – provide the subtotal and total price, respectively for that item line
  • variation_id is included if the product is a variation of a variable product

Getting coupon information from WC_Cart

WooCommerce’s WC_Cart class provides methods for accessing coupon information, which is essential for managing discounts and promotions:

get_applied_coupons() method

The get_applied_coupons() method returns an array of coupon codes that have been applied to the cart. It’s useful for displaying a list of used coupons to the customer or for logging and analysis purposes.

Here is how you can use this method:

$applied_coupons = WC()->cart->get_applied_coupons();
foreach ($applied_coupons as $coupon) {
  echo "Applied Coupon: " . $coupon . "<br>";
}

In this example, we iterate over the array of coupon codes returned by get_applied_coupons() and display each one.

get_coupon_discount_amount() method

The get_coupon_discount_amount() method retrieves the total discount amount for a specific coupon applied to the cart. It accepts two parameters:

  • $code (string, required) – the coupon code
  • $ex_tax (bool) – whether to exclude or include tax. Defaults to true.
$coupon_code = 'welcome';
$discount_amount = WC()->cart->get_coupon_discount_amount($coupon_code);
echo "Discount for coupon $coupon_code: $discount_amount";

In this snippet, we’re fetching the discount amount for a coupon code ‘welcome’. This method is particularly useful for detailed breakdowns of discounts from specific coupons, allowing customers to see how much they’ve saved from each coupon.

How to get cart customer

The get_customer() method in the cart class can be used to retrieve the customer data. When invoked, this method returns an instance of the WC_Customer class, which contains various details about the customer, including the customer’s billing and shipping details.

For customers who are logged in, the WC_Customer instance will have the ID property set to their user ID in WordPress. If the customer is not logged in, the ID property is set to 0. This differentiation is crucial for handling guest checkouts and registered user checkouts differently.

$customer = WC()->cart->get_customer();

if ( $customer->get_id() != 0 ) {
  // Actions for logged-in users
  echo "Customer ID: " . $customer->get_id();
} else {
  // Actions for guests
  echo "Guest Checkout";
}

In this code snippet, we retrieve the customer object using the get_customer() method. We then check if the get_id() method on the $customer object returns a non-zero value. A non-zero value indicates a logged-in user, while a zero value signifies a guest user.

Price-related methods in WooCommerce WC_Cart

WooCommerce’s WC_Cart class provides several methods to handle different aspects of pricing for the entire cart. These methods are essential for displaying accurate pricing information to the customer throughout the shopping process, from initial item selection to the final checkout.

  • get_subtotal() – returns the subtotal of all items in the cart before any discounts or taxes are applied. It sums up the cost of all items without considering discounts or shipping costs.
  • get_total() – provides the final total cost of the cart, including taxes and discounts. This is the amount the customer needs to pay.
  • get_discount_total() – returns the total amount of discount applied to the cart. It sums up all the discounts applied to the cart items.
  • get_shipping_total() – gives the total shipping cost for the cart. It’s relevant when shipping charges are applicable.

In the following example you can see how to use these methods:

echo "Cart Subtotal: " . WC()->cart->get_subtotal() . "<br>";
echo "Cart Total: " . WC()->cart->get_total() . "<br>";
echo "Total Discount: " . WC()->cart->get_discount_total() . "<br>";
echo "Shipping Total: " . WC()->cart->get_shipping_total();

Tax-related methods in WC_Cart class

When dealing with taxes in a WooCommerce cart, the WC_Cart class offers several methods to retrieve tax-related information accurately. These methods are essential for handling the cart’s tax components for the subtotal, total, discount, and shipping charges.

  • get_subtotal_tax() – method returns the total tax amount applied to the cart’s subtotal. This is the tax amount before discounts are applied to the cart items.
  • get_total_tax() – provides the total tax amount for the entire cart, considering all items, discounts, and shipping. It reflects the tax included in the final total amount payable by the customer.
  • get_discount_tax() – calculates the total tax amount saved due to discounts applied to the cart. It represents the tax difference between the original and discounted prices.
  • get_shipping_tax() – gives the total tax amount applied to the shipping charges of the cart. This is relevant in scenarios where shipping costs are taxable.

Here’s an example that uses these tax-related methods together:

$subtotal_tax = WC()->cart->get_subtotal_tax();
$total_tax = WC()->cart->get_total_tax();
$discount_tax = WC()->cart->get_discount_tax();
$shipping_tax = WC()->cart->get_shipping_tax();

How to view and search customer carts

The Users Insights plugin provides some WooCommerce cart features that can help you explore your users’ cart information. First of all, there are filters in the user table that allow you to segment your customers based on their carts and items in carts, including:

  • Cart has items filter – showing customers who have items in their cart
  • Cart is empty filter – showing customers who don’t have any items in their cart
  • Has product in cart filter – showing customers who have a selected product in their cart

Find WooCommerce users who have items in cart

Find WooCommerce customers by products in cart

Additionally you can explore each customer’s cart in the user profile section:

To learn more, head over to the WooCommerce integration page.

Conclusion

This article covers the essential functionalities of the WooCommerce WC_Cart class. It explains how to retrieve the current cart, check for items, manage pricing, taxes, and coupons, and understand customer data. By using these methods properly, you’ll find that they not only enhance the user experience but also provide a solid foundation for customizing and extending WooCommerce to meet specific e-commerce needs.