How to Show Number of Items & Total in WooCommerce Cart Section?

  • May 28, 2018
  • Ashish Shah
  • WordPress
  • No Comments
How-to-Show-Number-of-Items-&-Total-in-WooCommerce-Cart-Section

There are a variety of reasons why a WooCommerce website owner may want to display the number of items in the cart.

For instance, some would like cart count and cart totals to be immediately visible to the online shoppers in the navigation bar, in a popup or reminder, or virtually anywhere else on the WordPress page.

Related Post

Allowing customers to get the total item quantity would give the customers a summary of their purchase without having to view the cart contents.

In the interest of effective user interface and user experience design, many WordPress theme developers have opted to integrate cart quantities and totals within the theme itself.

However, since not all WordPress themes are designed with full WooCommerce support, there are instances when one would want to add such functionality programmatically, by making some modifications to the WordPress theme files and adding the necessary WordPress theme hooks and functions.

Following code for helpful study:

Today, we will learn how to display the number of items in the cart. The following code snippet can be used to get the number of items in the WooCommerce cart anywhere you want to display it on your website. You can place it anywhere in your theme’s files where you want to display it.

<?php global $woocommerce; ?>
<a class="cart-contents" href="<?php echo $woocommerce->cart->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(); ?>

Conclusion:

What the code above does is that it dynamically creates text containing the number of items in the cart, separated by a dash. This text would link to the shopping cart itself, enabling the users to view the entirety of the cart contents.

While the code snippet presented above may be used anywhere within the theme, for this example, we will add the cart information just below the navigation menu. To do this, we will edit the header.php file.

 

About The Author

Avatar

Ashish Shah

Ashish Shah is the Founder and CEO of NCode Technologies, Inc. a leading Web & Mobile App Development Company based in India. He is the chief mentor and strategist with over 10+ years of experience in providing various IT solution to different industries. He also likes to share his view on different technology and marketing techniques via different blogs and articles.

Leave a Reply

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