Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
WooCommerce - Show number of items in cart and total
<a class="cart-contents" 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>

If I'm using a child theme, where do I need to place the above code? I'm using Canvas theme.

Thanks so much! I wish they would improve their documentation to make this stuff a little easier to find...

Hi, tnx for the code.
how can I hide the word "item"/"items" and make it display only the number alone?

I need to use the total number of items in cart for my new shipping method, but I dont understand how to use this for that. Using only $woocommerce->cart->cart_contents_count doesnt seem to work?

Perfect! Thanks a million!

This is exactly what I was looking for. Thanks!

thank you so much boss! feeling happy..:-)

thanks

Can this apply in email template?

ajithrn commented Apr 9, 2015

thanks 😄

Hi ! How can I get the number of items of Current Product in cart at product single page?????
like, in my website
http://cartridgemedia.co.uk/beta/shop/brother/compatible-brother-dr2100-brother-dr-2100/
I need to show it in black strip at the right box

Thanks

I use this way

global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
if ($count > 0) {
echo '';
echo $count;
echo '';
}

Hello, I hope this is in the right thread...I'd like to display the cart total in the menu bar, but on both a main AND subdirectory site. Woocommerce is installed on the main site: https://stephaniedaviesarai.com - from where it will be possible to buy a book.

I've had to create a mirror install in a sub-directory (which looks identical) in order for my client to have two distinct blogs. This is effectively a one-page site at http://stephaniedaviesarai.com/cwc/cwc-blog/

The problem is that when you move to the second blog, you lose the cart. I know I can just have a straight link to the cart on the main site, but I lose the total in cart.

I'm look to put Your Cart - $20.00 in both menus. Can I do that?

Thanks for any and all help!

Great!

WOW someone out there thought that a plugin would be better, this works perfect.

edisonrd commented Dec 4, 2016

does not work as a dropdown

Doesn't update on cart page while updating products. need to refresh for new updated count.

morenojavier commented Jun 13, 2017

Perferct works. I share a function that conditions if you do not have products in the cart, do not show us anything.


<?php if( WC()->cart->get_cart_contents_count() > 0){ ?>
  <li>
    <a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View my car' ); ?>"><?php echo sprintf ( _n( '%d', '%d', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?>
    </a>
  </li>
  <? } ?>

Here is a plugin that allows you to add a shortcode displaying cart count and total: https://wordpress.org/plugins/woo-cart-count-shortcode/

Hey guys, what if I want to show the amount of unique items in cart? any hint?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment