Website Building » WooCommerce » How Do I Get a Cart Count in WooCommerce?

How Do I Get a Cart Count in WooCommerce?

Last updated on January 13, 2023 @ 12:11 pm

WooCommerce is a powerful eCommerce plugin that helps you turn your WordPress site into an online store. One of the features that WooCommerce offers is the ability to keep track of the number of items in a customer’s shopping cart.

There are two ways to view the cart count in WooCommerce. The first way is to go to the WooCommerce settings page and click on the “Advanced” tab.

From there, you will see the “Cart Count” setting. You can then choose to either display the cart count as a link or as a number.

PRO TIP: If you are using WooCommerce to manage your online store, you may be wondering how to get a cart count. This article will explain how to do this.

When using WooCommerce, it is important to remember that the cart count is not updated in real time. This means that if you add an item to your cart, the cart count will not update until the page is refreshed.

It is also important to remember that the cart count only reflects the items in your cart at the time the page was last refreshed. If you remove an item from your cart, the cart count will not update until the page is refreshed again.

If you need an accurate count of the items in your WooCommerce cart, it is best to refresh the page before checking the cart count.

The second way to view the cart count is to add the following code snippet to your theme’s functions.php file:

function WooCommerce_cart_count() {
global $woocommerce;
echo $woocommerce->cart->cart_contents_count;
}
add_action( 'woocommerce_before_cart', 'woocommerce_cart_count' );

This code snippet will display the cart count as a number next to the shopping cart link on your site.

You can also use this code snippet to style the cart count display. For example, you could wrap it in a span element with a class of “cart-count”:

function WooCommerce_cart_count() {
global $woocommerce;
echo '<span class="cart-count">' . $woocommerce->cart->cart_contents_count . '</span>';
}
add_action( 'woocommerce_before_cart', 'woocommerce_cart_count' );
Drew Clemente

Drew Clemente

Devops & Sysadmin engineer. I basically build infrastructure online.