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>
@chirag-v

This comment has been minimized.

Show comment Hide comment
@chirag-v

chirag-v May 28, 2013

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

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

@EvanAgee

This comment has been minimized.

Show comment Hide comment
@EvanAgee

EvanAgee Jul 23, 2013

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

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

@guygolan

This comment has been minimized.

Show comment Hide comment
@guygolan

guygolan Aug 19, 2013

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

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

@danielholm

This comment has been minimized.

Show comment Hide comment
@danielholm

danielholm Aug 23, 2013

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?

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?

@Swingline0

This comment has been minimized.

Show comment Hide comment
@Swingline0

Swingline0 Mar 6, 2014

Perfect! Thanks a million!

Perfect! Thanks a million!

@mohsinoffline

This comment has been minimized.

Show comment Hide comment
@mohsinoffline

mohsinoffline May 24, 2014

This is exactly what I was looking for. Thanks!

This is exactly what I was looking for. Thanks!

@minitpatil

This comment has been minimized.

Show comment Hide comment
@minitpatil

minitpatil Jun 7, 2014

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

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

@amaisbajwa

This comment has been minimized.

Show comment Hide comment
@amaisbajwa

amaisbajwa Dec 14, 2014

thanks

thanks

@elvinlee

This comment has been minimized.

Show comment Hide comment
@elvinlee

elvinlee Jan 20, 2015

Can this apply in email template?

Can this apply in email template?

@ajithrn

This comment has been minimized.

Show comment Hide comment
@ajithrn

ajithrn Apr 9, 2015

thanks 😄

ajithrn commented Apr 9, 2015

thanks 😄

@WaqarWebz

This comment has been minimized.

Show comment Hide comment
@WaqarWebz

WaqarWebz Jul 15, 2015

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

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

@eliasfaical

This comment has been minimized.

Show comment Hide comment
@eliasfaical

eliasfaical Sep 11, 2015

I use this way

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

I use this way

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

@StrongEagle

This comment has been minimized.

Show comment Hide comment
@StrongEagle

StrongEagle Oct 28, 2015

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!

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!

@gersonbarbosa7

This comment has been minimized.

Show comment Hide comment
@gersonbarbosa7

gersonbarbosa7 Aug 17, 2016

Great!

Great!

@alexapgutierrez

This comment has been minimized.

Show comment Hide comment
@alexapgutierrez

alexapgutierrez Sep 7, 2016

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

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

@edisonrd

This comment has been minimized.

Show comment Hide comment
@edisonrd

edisonrd Dec 4, 2016

does not work as a dropdown

edisonrd commented Dec 4, 2016

does not work as a dropdown

@deepikasinglay

This comment has been minimized.

Show comment Hide comment
@deepikasinglay

deepikasinglay Feb 7, 2017

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

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

@morenojavier

This comment has been minimized.

Show comment Hide comment
@morenojavier

morenojavier 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>
  <? } ?>

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>
  <? } ?>

@corydbrown

This comment has been minimized.

Show comment Hide comment
@corydbrown

corydbrown Sep 17, 2017

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

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

@bruno-rodrigues

This comment has been minimized.

Show comment Hide comment
@bruno-rodrigues

bruno-rodrigues Oct 21, 2017

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

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

@dudur

This comment has been minimized.

Show comment Hide comment
@dudur

dudur Nov 27, 2017

@ morenojavier
Works like a charm - Thanks !

dudur commented Nov 27, 2017

@ morenojavier
Works like a charm - Thanks !

@anonymousguyx

This comment has been minimized.

Show comment Hide comment
@anonymousguyx

anonymousguyx Dec 24, 2017

Thanks dude!
No mini cart cache problem now 🗡

Thanks dude!
No mini cart cache problem now 🗡

@shaileshgajare

This comment has been minimized.

Show comment Hide comment
@shaileshgajare

shaileshgajare Mar 13, 2018

how to cart in product count in woocommerce

how to cart in product count in woocommerce

@shaileshgajare

This comment has been minimized.

Show comment Hide comment
@shaileshgajare

shaileshgajare Mar 13, 2018

my problem solve

my problem solve

@Aeonexe

This comment has been minimized.

Show comment Hide comment
@Aeonexe

Aeonexe Apr 3, 2018

What about displaying the total of products without variations. i.e. I have 3 products and one of those have 2 variations so WC()->cart->get_cart_contents_count() would give 5 items. But I only want to count the 3 main products without variations.

Aeonexe commented Apr 3, 2018

What about displaying the total of products without variations. i.e. I have 3 products and one of those have 2 variations so WC()->cart->get_cart_contents_count() would give 5 items. But I only want to count the 3 main products without variations.

@walworthvalves

This comment has been minimized.

Show comment Hide comment
@walworthvalves

walworthvalves Apr 5, 2018

I have a problem in internet explorer, when I update the number of products on the cart page if it does, but in internet explorer 11 is not updated, I know, nobody uses internet explorer 11, except my client.

walworthvalves commented Apr 5, 2018

I have a problem in internet explorer, when I update the number of products on the cart page if it does, but in internet explorer 11 is not updated, I know, nobody uses internet explorer 11, except my client.

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