How to Show the number of items in cart in WooCommerce

Show number of items count cart in WooCommerce
2770 Views
4.1
(9568)

If you run an online store using WooCommerce, it’s important to provide your customers with a seamless shopping experience. One way to do this is by displaying the number of items in their shopping cart.

By showing customers the number of items in their cart, they can quickly and easily keep track of what they’ve added, helping to prevent any confusion or frustration during the checkout process.

Cart Total: <?php echo WC()->cart->get_cart_contents_count() ?>
Cart Total: <?php echo count( WC()->cart->get_cart() ) ?>

WooCommerce number of items count in the cart

<?php
global $woocommerce;
echo 'Cart Total: ' . $woocommerce->cart->get_cart_contents_count();
?>
<?php
global $woocommerce;
echo 'Cart Total: ' . count( $woocommerce->cart->get_cart() );
?>
<?php
// echo $woocommerce->cart->cart_contents_count;
Output : 2

// echo count( WC()->cart->get_cart() );
Output : 2
?>

Count every product just once using get_cart_contents_count()

<?php
add_filter( 'woocommerce_cart_contents_count', 'cxc_get_cart_count_call_back' );

function cxc_get_cart_count_call_back( $count ) {
	return count( WC()->cart->get_cart() ); 
}
?>

Excluding a certain product from the cart count

<?php
add_filter( 'woocommerce_cart_contents_count', 'cxc_get_cart_count_excluding_product_call_back' );

function cxc_get_cart_count_excluding_product_call_back( $count ) {
	//return count( WC()->cart->get_cart() ); 

	$count = 0;
	if( WC()->cart->get_cart() ){	
		foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		// just skip the loop iteration if it is a specific product_id
			if( 181 === $cart_item[ 'product_id' ] ) {
				continue;
			}
			$count++;
		}
	}

	echo $count;
}
?>

When a product is added to the cart, the cart count is updated

When a product is added to the cart using Ajax, the previously inserted cart count, budget, or cart menu should be updated.

To begin, turn on the WooCommerce Ajax add-to-cart option. To access the following option, go to WooCommerce > Settings > Products. I double-checked and saved it.

WooCommerce offers a means for us to manage it through cart fragments, namely the really handy hook woo commerce adds to cart fragments.

<a class="cxc-cart" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> – <?php echo WC()->cart->get_cart_total(); ?></a>

First, locate the functions.php file from the currently active theme. If it’s a parent theme, I recommend creating a child theme and making the required modifications. This hook can also be utilized within your plugin’s main file.

<?php
add_filter( 'woocommerce_add_to_cart_fragments', 'cxc_woocommerce_header_add_to_cart_fragment' );

function cxc_woocommerce_header_add_to_cart_fragment( $fragments ) {
	global $woocommerce;

	ob_start();

	?>
	<a class="cxc-cart" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> – <?php echo $woocommerce->cart->get_cart_total(); ?></a>

	<?php
	$fragments['a.cxc-cart'] = ob_get_clean();
	return $fragments;
}
?>

Also read

How to Fix parseJSON Not Working on iPad’s Safari Browser

How to add custom fields in admin edit user and profile in WordPress

How to Customize Billing and Shipping Address Fields in WooCommerce

FAQs

How can I show the number of items in the cart in WooCommerce?

You can display the number of items in the cart by using a code snippet or a plugin. The code snippet can be added to your theme’s functions.php file.

Can I display the cart item count in the header or menu of my website?

Yes, you can display the cart item count in the header, menu, or any other location on your website using a code snippet or a plugin.

How useful was this blog?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 9568

No votes so far! Be the first to rate this blog.

Leave a comment

Your email address will not be published. Required fields are marked *