These are 3 steps to add a WooCommerce cart icon with the cart count to your theme. The number of items will be updated automatically as items are added to cart.
To avoid errors, this will first check if WooCommerce is active. That way, it doesn’t hurt to add this to any theme regardless of whether WooCommerce plugin is active or not.
- Add the HTML to your theme’s template file where you want the cart icon to appear. The cart icon will only appear if WooCommerce is active.
<?php if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { $count = WC()->cart->cart_contents_count; ?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php if ( $count > 0 ) { ?> <span class="cart-contents-count"><?php echo esc_html( $count ); ?></span> <?php } ?></a> <?php } ?>
You can either add the above directly into your theme template, or you can add it with one of your theme’s template hooks. For example, if your theme’s header has a hook like,
do_action( 'your_theme_header_top' );
, then you can add the above code to your functions file like this:/** * Add Cart icon and count to header if WC is active */ function my_wc_cart_count() { if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { $count = WC()->cart->cart_contents_count; ?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php if ( $count > 0 ) { ?> <span class="cart-contents-count"><?php echo esc_html( $count ); ?></span> <?php } ?></a><?php } } add_action( 'your_theme_header_top', 'my_wc_cart_count' );
- Add the following to your functions. This ensures the cart will be updated immediately as items are added to the cart.
/** * Ensure cart contents update when products are added to the cart via AJAX */ function my_header_add_to_cart_fragment( $fragments ) { ob_start(); $count = WC()->cart->cart_contents_count; ?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php if ( $count > 0 ) { ?> <span class="cart-contents-count"><?php echo esc_html( $count ); ?></span> <?php } ?></a><?php $fragments['a.cart-contents'] = ob_get_clean(); return $fragments; } add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
-
Add this CSS to use WooCommerce’s native shopping cart icon. Adjust as needed to fit nicely into your theme. You can also use your own icon from a custom icon font. To use a different icon, change ‘WooCommerce’ to the name of your icon font, and change the
e01d
on line 3 to your icon’s Unicode. For example, if you already use Font Awesome, change line 2 tofont-family:FontAwesome;
, and line 3 tocontent: "\f07a";
to use Font Awesome’s cart icon.See how to add CSS.
Also, you may want to edit line 15 and 16 below to change the color of the cart count and background color of the bubble.
.cart-contents:before { font-family:WooCommerce; content: "\e01d"; font-size:28px; margin-top:10px; font-style:normal; font-weight:400; padding-right:5px; vertical-align: bottom; } .cart-contents:hover { text-decoration: none; } .cart-contents-count { color: #fff; background-color: #2ecc71; font-weight: bold; border-radius: 10px; padding: 1px 6px; line-height: 1; font-family: Arial, Helvetica, sans-serif; vertical-align: top; }
That’s it. Preview your store and add items to the cart to see this in action.
Guest
November 11th, 2015 at 10:19 am
Hi, Thank you very much. you saved a lot of time and energy for me. God bless you
Cléber Oliveira
November 12th, 2015 at 9:57 am
Thanks! It gave me the right direction for what I wanted. I’ve used it along with some refs. on storefront and now I have my cart widget working sweet as on storefront 🙂
Dũng Hoàng
December 9th, 2015 at 5:17 am
thanks u very much :3
Keith
April 12th, 2016 at 6:36 am
Hi
I simply want to use section 3 in my functions file to change my icon, I have changed it to FontAwesome and put the correct code in.
My site already has a top bar with a mini cart in and its that icon i am trying to change?
Any help would be gladly received!
Isabel
April 13th, 2016 at 4:00 pm
You’d have to change the CSS selector “.cart-contents” to match and override the one for your existing icon. You can see that in your browser by hovering over the icon, right-clicking, and “Inspect Element.”
gony_x
April 26th, 2016 at 10:20 am
Thank you very much!!!!! 🙂
Jeina
May 18th, 2016 at 12:43 pm
OMG thank you so so much!!!
Louie
June 15th, 2016 at 2:19 pm
thanks Isabel, I found this very useful and a good starting point for looking at adding the mouseover cart fragment (the drop down div that appears on many themes)
A.Torres
July 2nd, 2016 at 4:00 am
Olá, muito obrigado por dispor seu código. Você é uma excelente Dev.
Hi, Thank you for your code, you is a excellent Developer 🙂
Adam Helman
August 27th, 2016 at 8:06 am
Hi
Great stuff thanks!
However, is there a way to make it so the cart is only visible if it’s not empty?
Thanks
Pradeep M
August 28th, 2016 at 11:46 am
Isabel,
This article is awesome and it saved me a lot of time. For some reason this segment caused some issues as it was ‘t work so I had to remove it
And it still works fine. Just one question though. How can we style the number of items so they appear as a bubble on the cart?
Many thanks and regards,
Pradeep
Isabel
September 27th, 2016 at 1:29 pm
Hi, I’ve updated the snippets on this page so that the number of items will appear as a bubble. (Also see the updated CSS to style the bubble color.) I hope this helps.
Pradeep M.
September 27th, 2016 at 1:38 pm
You are a star 🙂
djramc
October 5th, 2016 at 7:05 am
Thank you very much!!!!!
Nick
October 28th, 2016 at 3:30 am
Code works really well, and instructions are clear and simple. Thanks for providing this!
Ryebo
November 4th, 2016 at 10:26 am
This works great however the cart shows up on every page in Firefox (which is what I want) but only on the woocommerce related pages in Chrome and Safari. Any idea on how to make sure it shows up on every page?
Ryebo
November 4th, 2016 at 10:41 am
Never mind, had to flush the cache in wordpress to get this to show
marianna
November 4th, 2016 at 3:58 pm
hi! thanks for help! I have an question.How can display the empty cart link,to custom page and not on shop page?
jurasjo
January 27th, 2017 at 7:53 am
Hi. I use that code and love it. I have noticed recently that sometimes when I’m entering my site (from different browsers and ip’s) there green indicator is showing 1 item in the cart. After entering the cart there is no item.
I use Autoptimize and WP Super Cache. Do You know if maybe this can be a potential problem?
Isabel
January 27th, 2017 at 2:15 pm
Autoptimize would not cause a problem with this. The cache is an issue. I’ve never used WP Super Cache, I use Hyper Cache, and I always add the WooCommerce Cart page, and the Checkout page, and product pages to the “List of URIs to Bypass” in the cache settings, like this:
This means that those pages will not be cached. This eliminates errors with the WooCommerce cart, for me. I have found, in my cases, that WooCommerce doesn’t work well with cache plugins. If you don’t mind having those pages not cached, then try adding that list to the WP Super Cache settings, if they have a setting for “Bypassing the cache.”
jurasjo
January 27th, 2017 at 3:59 pm
Thank You. I will try this solution.
ash
February 10th, 2017 at 7:46 am
Is this solution using/hitting admin-ajax.php ?
I’m trying to avoid this.
Isabel
February 10th, 2017 at 3:12 pm
Short answer: no.
This code doesn’t include any AJAX/jQuery/JavaScript that do the actual work. WooCommerce core does the actual work: WooCommerce runs their AJAX script anyway when items are added to the cart. (My code here simply adds the necessary HTML elements to capture their AJAX response for the cart count.)
But WooCommerce doesn’t use
admin-ajax.php
for adding the items to the cart. They created their own AJAX endpoints, and the one they use here is?wc-ajax=add_to_cart
instead ofadmin-ajax.php
.Hope that helps.
ash
February 11th, 2017 at 6:51 pm
Thank you Isabel for the quick clarifications.
One final query, How do I remove the brackets around the counter? When I insert your code snippets, I see the following in cart: (5).
Thanks again. You are awesome!
Isabel
February 13th, 2017 at 9:17 am
Thank you. This code doesn’t add any brackets. I am guessing that your theme (or plugin) is possibly adding a filter for the cart count, to add the brackets. If you find this somewhere in your theme:
woocommerce_cart_contents_count
, that would be modifying the cart count.ash
February 16th, 2017 at 12:45 pm
Thanks for the response. This solution works great!
…but with W3TC cache plugin and cloudflare installed, most of the pages do not display the correct count (- display 0). I’ve only excluded cart page from cache, therefore it displays correctly. I cannot exclude header.php in w3tc cache plugin.
Any ideas? your thoughts? Thank you
djramc
February 12th, 2017 at 4:24 pm
Hi, Thank you very much. you saved a lot of time and energy for me. God bless you
bewakingscamera
UVScott
February 14th, 2017 at 6:24 pm
Handy code snippet. I was able to incorporate it into the Storefront theme, adding Font Awesome’s cart icon, with minimal tweaks.
Thank you.
Tim
February 22nd, 2017 at 11:32 am
I can’t get this to work in a WordPress multisite setup.
Marc
June 12th, 2018 at 2:46 pm
I was able to get it to work by removing the if statement to check if the plugin was active. I’m guessing that php would be different in a multisite setup. Just use the code like this:
JOSE
March 7th, 2017 at 10:56 pm
I do not understand how to do this from storefront theme. I’m a developer, just not WP one. Any help? I thik i just need to know what hooks to use.
Isabel
March 8th, 2017 at 11:14 pm
I don’t have experience with the Storefront theme, but doesn’t it have its own shopping basket icon? I took a look, and you can use the
storefront_header
hook. So, you can add this to the Storefront theme by using the second example from #1, above, but change line 19 to this:If you want to align it to the top-right corner, add this CSS:
Hope that helps.
JOSE
March 9th, 2017 at 9:58 pm
I briefly tried that with no luck. This already had worked for me anyways.
functions.php:
And a customized version of your css.
Andy
March 22nd, 2017 at 4:44 pm
Jose, can you please share your customized version of the CSS?
JOSE
March 23rd, 2017 at 7:17 pm
akash
March 20th, 2017 at 4:27 am
Thanks a ton!!
UVScott
March 22nd, 2017 at 6:36 am
Hello,
This is the snippet that worked for me in Strore Front. Yes. It already has its own cart but I prefer this one. Perhaps someone else might find it useful, as well.
Steven Paul
April 6th, 2017 at 9:28 pm
Hi Isabel thanks for this site!
I’m in a slightly different scenario… I have setup an online menu using the WPPIZZA plugin (X-theme)
I pretty much have the shopping and checkout experience looking and working the way I want, but I still want the cart icon (in the nav bar) to display the total number of products added.
How can I do this without disrupting the work I’ve done?
I have Woocommerce installed, but do not know much about it.
I will have at least 4 menu pages, ( all should display the cart) but only one is active now: http://colorsstudios.com/?page_id=3230
Note: There is a shopping cart in the side bar which is visible on wide displays, so this functionality is mostly for mobile users, although I wish to have it visible on all platforms.
Husnain
May 4th, 2017 at 2:42 pm
Hi
Great work
what i do if want to add some text with number for example “2 Items”?
Isabel
June 5th, 2017 at 7:46 pm
You can add text after the number by inserting this (note the singular “item” and plural “items”):
This must be inserted into the beginning of line 6, in code block Number 1. Also, into the beginning of line 10, in Number 2.
Pierre
May 22nd, 2017 at 3:53 pm
Do you know how you can display 0 if there are no items in the cart?
I changed
to
And the zero shows up momentarily, and then disappears like some jquery removes it from the DOM. I’m guessing this is some js in woocom?
Isabel
June 5th, 2017 at 7:34 pm
Did you make the change in both places?
if ( $count > 0 ) {
needs to be changed in both places: code block Number 1, line 5, and code block Number 2, line 9. Hope that helps.Steven Roberts
July 17th, 2017 at 11:00 am
Works a real treat
Ahmed
October 24th, 2017 at 9:37 am
I usually don’t comment but this has saved me so much time and it works like charm! Thank you!
CJ
October 26th, 2017 at 4:46 am
Very helpful code, thank you!
It seems that WC()->cart->get_cart_url() has been deprecated since WC version 2.5? The console reports to replace this with wc_get_cart_url.
Pooja Dahri
January 19th, 2018 at 1:44 am
Hi, Thank you very much. you saved a lot of time and energy for me. God bless you
Antonio
January 21st, 2018 at 2:29 pm
Thank you so much! It worked very well for the theme I’m building.
Tayde Sandoval
January 23rd, 2018 at 3:00 pm
Thanks a lot!
I spent HOURS looking for a solution and your code totally worked just fine at the first try!
<3 <3 <3 <3
Jacques
February 25th, 2018 at 3:52 pm
Thank you, works out of the box … I made my own HTML/CSS modifications and missed the note about keeping both files’ markup in the same state so the cart count wouldn’t update.
Clear and simple – much appreciated!
Adal Bermann
April 3rd, 2018 at 5:23 pm
WC()->cart->get_cart_url() is deprecated, use wc_get_cart_url() instead
Also I chose to link to the shop page if cart is empty:
Sara
June 17th, 2018 at 12:52 am
solved a big problem, thanks you so much 🙂
Kevin
July 22nd, 2018 at 1:37 am
Awesome. So clear and easy to implement. Thanks for writing this up!
Guilherme Neumas
August 16th, 2018 at 3:53 pm
Wonderful! Straight to the point!
Nuno Morais Sarmento
September 24th, 2018 at 8:37 am
Thank you for sharing this WP code snippet.
Otto
November 20th, 2018 at 2:49 pm
Thanks for the solution!
Phillipe Calmet Williams
July 31st, 2019 at 10:05 am
Thank you for this code. It is very useful.
armin
August 19th, 2019 at 4:33 am
still working, saved me so much time! thank you for sharing!
I-WEB Kamil Kostelecki
November 20th, 2019 at 6:03 am
Great! Thanks, man!
g1edrius
March 18th, 2020 at 7:14 pm
Thanks, you saved a lot of my time! And by the way it works with woocommerce 4.0
Mohit
April 9th, 2020 at 9:19 am
Worked Perfectly fine for me, Thanks a ton.
zulfikar wijaya
April 19th, 2020 at 11:02 am
From Indonesia just wanna say, matur nuwun (Thank You)!
Omar giancarlo
November 10th, 2020 at 12:45 am
Thank you very much!! Working fine on Divi Site.