In this tutorial, we will learn how to get woocommerce cart items info like cart items total, cart items count number, cart fee, etc.
WooCommerce is the most popular platform to build an eCommerce website. It is a WordPress plugin and you can install it and start building your online store. WooCommerce is a very flexible and customizable plugin and it allows you a lot of customizations, you can check in the woocommerce category.
To customization in woocommerce, you have to deal with template files and can also use the actions/hooks. So if you want to customize your cart page then you can use the woocommerce action/hooks. There are lots of woocommerce actions/hooks appropriate to the different sections under woocommerce platform.
Following are the action/hook-based customizations on the cart page.
Change Proceed to Checkout Button Text in WooCommerce?
Change Place Order Button Text in WooCommerce?
Add WooCommerce Cart Icon in Menu with Item Count?
Change Price of Specific Product and Quantity in Cart?
So, in this tutorial, we will get woocommerce cart items info to play with them. You can make any condition based on any specific cart item value.
Get WooCommerce Cart Items Info
We will use the global
WC() function or global
$woocommerce variable to get the cart item’s details. You can use these variables anywhere in the woocommerce platform.
You can also use the
$cart variable object in the woocommerce actions/hooks. But in case you don’t have access to use the
$cart variable then you can use the
$woocommerce global variable to access the cart content.
Using the WC()->cart
WC()->cart method to get woocommerce cart item data. Cart page use this method to load all the values on the cart page e.g. woocommerce get cart item totals, subtotal, get cart items to count, get items price, etc.
You can use this method anywhere on the frontend section of woocommerce website and customize the woocommerce functionality as you want.
See the following methods are available for the cart page using the
For all $cart items
The following code line will get all cart items. It will return the object, you can loop over to get the specific item’s value.
For $cart totals
Use the following
WC()->cart methods to get the $cart totals. You can get the total based on different scenarios like cart shipping totals, cart taxes totals, cart fee totals, cart discount totals, etc.
For $cart conditionals
Using the following methods, you can make the conditionals statements with cart values. You can perform specific functionality-based specific conditions using the following $cart methods.
For $cart customer billing
If you want to get the customer billing details on the cart page or checkout page then use the following billing methods of the $cart page. You can get the customer billing first name, last name, email, phone, country, etc.
For $cart customer shipping
If you want to get the customer shipping details on the cart page or checkout page then use the following shipping methods of the $cart page. You can get the customer shipping first name, last name, address, country, etc.
Other $cart methods
Following are the more relatable methods that you can use if you need them.
Loop over $cart items
If your cart has multiple items then you can loop through them and get the details of items using the further product-based function. We will discuss these functions in another tutorial, keep checking and join the feed.
For $cart meta data
To get the $cart meta data, use the
WC->cart->get_cart() and loop it and then you can use the
wc_get_formatted_cart_item_data() function by passing the
Using the $woocommerce
You can also use the
$woocommerce global variable to get the values of cart content. You can use it anywhere in the template file or in the functions.php file.
WC() function with the $woocommerce variable. That’s it. But before using the $woocommerce variable, you have to define it as global using the
See the some of following examples.
Thus, you can use all $cart methods with the
$woocommerce global variable. If you are customizing the woocommerce functionality then I would recommend using a child theme to prevent the loss of custom code while updating the theme or plugin.
Some Most Useful $cart Methods
Let’s see the some of above methods that are very useful while we doing customization in woocommerce. We will see the example with both
$woocommerce global variable.
Get Total Number of WooCommerce Cart Items
It is very simple to display the total number of cart items. You can also say the cart quantity. See the example below.
You can use the
_n() translation function for plural or singular form for product quantity while displaying the count at the frontend. It means, if the cart product has 1 product then it will show a count like “1 product” and if have product more than one then shows the “2 products“.
Get WooCommerce Cart Item Price
Get WooCommerce Cart Item Subtotal
Get WooCommerce Cart Count via Ajax
This is the scenario if you are building custom functionality to display the cart count in the menu or anywhere else. So when the user adds the product into the cart then update the cart fragments automatically and get the cart count.
For this, we can use the
woocommerce_add_to_cart_fragments magic filter hook. It will give you the updated refresh count of the cart object.
See the following hook function code example.
I also explained in this tutorial How to Add WooCommerce Cart Icon in Menu with Item Count?
See Also: How to Get WooCommerce Order Details?
So, you learned about how to get woocommerce cart items info to customize the woocommerce functionality. You learned how to get the total number of cart items, cart item price, cart item fees, cart item total and subtotal, etc.
You also learned about the most useful woocommerce cart methods to get the cart object and see how to update the cart item count via ajax while a new product adding to the cart.
If you like this information, share it with others so they can also take advantage of this piece of information. And if you have any questions please put them in the comment section. I will try to help you with that.