Add WooCommerce Cart Icon to Menu with Cart Item Count

Here is a way to add WooCommerce cart icon to a menu with the cart item count, it links to the cart page and displays the number of items that have been added to the cart.

Cart Icon In Menu

The code uses three functions which need to be added to functions.php and some CSS in style.css.

(If you just want the icon only – the process is a lot easier)

Create a shortcode for the WooCommerce cart, this can be appended to a menu or just added anywhere in your theme templates or hooked in with actions in the theme.

Create Shortcode

The shortcode, in this case, would be [woo_cart_but]

Filter to enable Ajax on the Cart Count

Add a filter to get the cart count to update when it changes using the Ajax fragments.

Add Cart Icon to an existing menu

The third code snippet it to add to a menu using a variant of wp_nav_menu_items the variant being to specify an actual menu in the filter, in the example the menu has an id of ‘top-menu‘ – so the filter name can be adjusted like so wp_nav_menu_top-menu_items

If your menu had the ID of primary-menu – the filter name would be changed to wp_nav_menu_primary-menu_items etc

(Add Cart Icon by itself)

If you wanted the Cart Icon by itself (not in a menu) – you can use the shortcode or for PHP template files you can use do_shortcode

<?php echo do_shortcode("[woo_cart_but]"); ?>

Add CSS

Finally, add some CSS…

That’s it a working icon with cart count items displaying.

Display Icon Only

If you don’t care for the cart count functionality the process is a lot easier, you simply add the Cart page to the desired menu in WordPress menus and add the icon code in the Navigation label.

Add Cart Woocommerce Menu

In the above, a Fontawesome 5 icon is being used as it is already loaded in the theme.

Ref

59 Comments

  1. Niteesh on June 29, 2023 at 12:24 am

    Thank you for writing this code, as it has helped me with my site.
    It all works well, except for one issue. When the user has items in the cart and increases/decreases the quantity while on the cart page, the $cart_count does not get updated until the page is refreshed.
    Any idea how to add this functionality and get it to update automatically?

  2. alex on February 22, 2023 at 8:59 am

    thanksssssss

  3. Michal on November 25, 2022 at 9:45 am

    Hi, the code works great! However, if there is no product in the cart, the cart icon will not appear at all. How to fix this?

  4. Dwayne on September 16, 2022 at 7:03 pm

    Got it! Thanks for taking the time to write this up and post it. I had to do some tweaks. “::marker” – had to set content:””;, and some small CSS tweaks to place the counter next to the basket.

  5. Gerardo on August 2, 2022 at 9:33 pm

    You are the guy!
    I just used for Hello Theme from Elementor.
    An easier way to add the icon in menu is using a plugin to add shortcodes in menu. The “Shortocode in Menus” plugin worked just fine for me.
    With this you can skip the Step: Add Cart Icon to an existing menu
    Bests!

  6. Rubb on February 14, 2022 at 8:37 am

    Nice, but where is the ul for the li?

  7. Alberto on January 12, 2022 at 4:52 pm

    Hi, great tutorial. i followed the steps and the only thing that is not working is the cart icon everthing else is. I get the number of items showing but not the image.

    Any suggestions?

  8. Alberto on January 12, 2022 at 11:15 am

    Hi, great tutorial. the only issue i am having is that it does not show the icon of the cart but only the number of items.

    any suggestions?

    thx

  9. Sam on November 3, 2021 at 10:56 pm

    Thank you so much for this!!! It took me so long to find a tutorial that didn’t require a plugin. This was exactly what I needed!

  10. Rémi on May 16, 2021 at 10:33 am

    Hello !
    I’ve tried it on Hello theme + Elementor and it worked fine.

    Now i’ve copy/paste the same code on another WordPress install with Storefront + Elementor with same CSS and it outputs html differently, like this :

    Cart icon
    0,00€
    0 article

    Here is a screenshot :

  11. Aviel on April 13, 2021 at 3:26 pm

    Hey, thanks for the code!!!
    i’m trying to do this with Hello theme and Elementor.
    I don’t know how to find the nav name.
    Did anypne was able to do this with Elementor?
    Thanks :)

  12. Bastian on March 15, 2021 at 10:16 am

    Hello

    I have the icon that appears in the menu, but I do not have the number of items in the icon inserted

    Can you help me?

    Thank you

  13. Rich Vicary on February 19, 2021 at 11:51 am

    Hi, I’m having difficulty whereby I see a bullet point with the cart count indented?

    I’ve tried amending the code with the advice above on one of the previous comments but I then get about 10/15 versions of the cart and counter. Appreciate any help you can give please.

    • Rich Vicary on February 19, 2021 at 11:57 am

      Right, I’ve resolved the bullet point by removing the but I now have a massive space between the trolley icon and the actual cart counter. Can you help with this please.

    • Ursula on February 23, 2021 at 10:33 am

      Hi,
      I have addes the three functions in the article to functions.php (changed p_nav_menu_primary-menu_items because I want to diosplay the cart in the main menu) and added the above mentioned custom CSS.

      Results:
      – no cart icon in the main menu
      – short code: cart color is pink, has a bullet point an the count numbder is not inside the icon.

      What ist wrong?

      https://test.barbaraskueche.de/

  14. Anton on February 18, 2021 at 8:14 am

    Thank you! Very useful! You are amazing!!! :)

  15. Alys on November 25, 2020 at 2:30 am

    This is wonderful. Thank you! What would one add in order to hide the whole icon (and count) if the cart is empty?

  16. Ubaid on November 18, 2020 at 12:53 pm

    Thank You Very Much

  17. Ninja on November 14, 2020 at 10:35 am

    Hey!

    Woundering if is possible to move the icon (with the item count function – not just the icon) right side of the SHOP name in the menu if I place the SHOP … lets say… in the middle and not at the end of the menu.

    Example:
    HOME / MEDIA / FISHING / UTILE / CONTACT / TALCIOC (icon here)

    TALCIOC being the SHOP.

    So… if I move TALCIOC before CONTACT the icon will remain where it is and will show after CONTACT.

    is there a way where I can call the icon to stick with a menu id or something?!

    https://angler.ro is the site where I want to implement this.

    Thank you!

  18. Peter on October 26, 2020 at 11:26 pm

    Hi Neil. Have spoken in a long while, found this one today, solved my problem – thanks!

    Hope you’re keeping well.

  19. Habi on October 22, 2020 at 4:00 pm

    the code worked just fine. awesome!!!
    I would also like to add a wishlist icon or button next to the basket. can you please help me through this. guidelines of what should I change in the above code should be fine for me.
    thanks in advance.

  20. Saman on October 14, 2020 at 3:38 pm

    And also line 3 :
    wp_nav_menu_items

    It worked for me.

  21. Saman on October 14, 2020 at 3:35 pm

    line 9 on the last code is wrong. it must be:
    $items .= do_shortcode(“[woo_cart_but]”);

  22. Erwin on October 9, 2020 at 7:59 pm

    Cool! Thnx for this! Is there a way to add a ‘current-page’ class when the user is actually on the cart page?

  23. Cordial on September 14, 2020 at 5:23 am

    Hello Sir,
    I’ve inserted the code, but nothing really happened. I believe I may be doing something wrong, so I thought to send you some details to know how you can help me:

    This are screenshots of my Menu– https://prntscr.com/ugt8hi

    My Menu Location– https://prntscr.com/ugt8zd

    And my Header Menu– https://prntscr.com/ugt9mj

    While I’ve implemented the code, the shortcode shows on my menu. Later, it’s no longer there.

    Please, How do I get the code to work as expected?

    Desperate to hear from you soon.
    Regards

  24. minesh on September 6, 2020 at 10:47 pm

    Hi, how would i add the cart total instead of the count

  25. herbyderby on July 29, 2020 at 9:41 pm

    This worked PERFECTLY. Just a little CSS tweaks for the site I’m working on, and I’m off and running! Thank you very much for this!

  26. Dustin on July 23, 2020 at 11:46 pm

    The issue I face is it gives me a weird style of cart and I want to add the same style as the menu one but for the life of me I can’t figure it out since I don’t know where to place the in the code listed above…

  27. Camilo Garcia on July 15, 2020 at 2:28 am

    Hey Daniel, thanks for sharing this, it is pretty awesome in fact

    I´ve a question. When you reply a comment above explaining how to remove the list-style is applying on the shopping cart, I get lost how to remove it:

    You said:

    “You have to remove both in short code. final result:

    cart_contents_count; // Set variable for cart item count
    $cart_url = wc_get_cart_url(); // Set Cart URL

    ?>
    //removedli
    0 ) {
    ?>

    //removed li
    <?php

    return ob_get_clean();

    }

    but this isn´t affecting the list-style image at all. Can you help me with this please

  28. Novacode on June 7, 2020 at 11:50 am

    Great! Thank you.

    For those who prefer the menu variant… just make a small change:

    This:
    *****
    function woo_cart_but_icon ( $items, $args ) {
    $items .= do_shortcode(“[woo_cart_but]”); ‘[woo_cart_but]’; // Adding the created Icon via the shortcode already created
    return $items;
    }

    to this:
    *******
    function woo_cart_but_icon ( $items, $args ) {
    $items .= do_shortcode(“[woo_cart_but]”); // Adding the created Icon via the shortcode already created
    return $items;
    }

    Then everything works as described.

  29. Timothy O'Brien on April 26, 2020 at 9:12 am

    Great, but wheres the plug in version of this for us armchair coders?

  30. Eve on April 15, 2020 at 8:26 pm

    I would like my cart icon to be next the the search in the upper right on the homepage. How do I get it there? Right now it wraps off the primary menu. Here is my code.

    add_filter(‘wp_nav_menu_items’,’sk_wcmenucart’, 10, 2);
    function sk_wcmenucart($menu, $args) {

    // Check if WooCommerce is active and add a new item to a menu assigned to Primary Navigation Menu location
    if ( !in_array( ‘woocommerce/woocommerce.php’, apply_filters( ‘active_plugins’, get_option( ‘active_plugins’ ) ) ) || ‘primary’ !== $args->theme_location )
    return $menu;

    ob_start();
    global $woocommerce;
    $viewing_cart = __(‘View your shopping cart’, ‘your-theme-slug’);
    $start_shopping = __(‘Start shopping’, ‘your-theme-slug’);
    $cart_url = $woocommerce->cart->get_cart_url();
    $shop_page_url = get_permalink( woocommerce_get_page_id( ‘shop’ ) );
    $cart_contents_count = $woocommerce->cart->cart_contents_count;
    $cart_contents = sprintf(_n(‘%d item’, ‘%d items’, $cart_contents_count, ‘your-theme-slug’), $cart_contents_count);
    $cart_total = $woocommerce->cart->get_cart_total();
    // Uncomment the line below to hide nav menu cart item when there are no items in the cart
    // if ( $cart_contents_count > 0 ) {
    if ($cart_contents_count == 0) {
    $menu_item = ‘‘;
    } else {
    $menu_item = ‘
    ‘;
    }

    $menu_item .= ‘ ‘;

    $menu_item .= $cart_contents.’ – ‘. $cart_total;
    $menu_item .= ‘
    ‘;
    // Uncomment the line below to hide nav menu cart item when there are no items in the cart
    // }
    echo $menu_item;
    $social = ob_get_clean();
    return $menu . $social;

    }

  31. Fred Best on March 2, 2020 at 11:48 pm

    Hi

    This all looks good, but there is a ‘bullet point’ and this indents the cart see here: https://www.screencast.com/t/lLqXL8R5

    How is this removed?

    • daniel on June 12, 2020 at 3:00 am

      You have to remove both in short code. final result:

      <?php // cart->cart_contents_count; // Set variable for cart item count
      $cart_url = wc_get_cart_url(); // Set Cart URL

      ?>
      <a class="menu-item cart-contents" href="” title=”My Basket”>//removedli
      0 ) {
      ?>

      //removed li
      <?php

      return ob_get_clean();

      }

  32. Radek on February 26, 2020 at 12:25 pm

    Great Work, Thanks!!

  33. Benjamin Hodge on February 21, 2020 at 12:13 pm

    Good morning. I added the 3 PHP snippets to my child theme’s functions.php file, and I added the CSS to the Additional CSS. In Appearance > Menus my top navigation menu is named TopNavMenu, and when I inspect the top navigation menu I see “menu-topnavmenu” as the id. Assuming that is correct, I updated the cart-add-menu.php to “add_filter( ‘wp_nav_menu_menu-topnavmenu_items’, ‘woo_cart_but_icon’, 10, 2 ); // Change menu to suit – example uses ‘top-menu'”.

    After doing that (and saving functions.php) I’m still not seeing the cart in the menu. What am I doing wrong? Am I overlooking something simple? Please help. Thank you.

  34. Hove on February 3, 2020 at 2:41 am

    My menu slug is top-navigation and I’ve added this code:
    add_filter( ‘wp_nav_menu_top-navigation_items’, ‘woo_cart_but_icon’, 10, 2 );
    I am setting a global variable in woo_cart_but_icon function to see if it’s being called and it’s not being called. Why?

  35. Bogdan on January 29, 2020 at 11:12 pm

    Thanks for the short code it looks great, but in my case it does not show number of items in the cart, only the cart icon.

  36. Matt on January 11, 2020 at 11:44 pm

    Thank you for this snippet – it works great.
    I’m wondering how to make it so the cart icon / shortcode only displays when something is added to cart, with AJAX.
    I modified the code so it only shows when there is an item but I don’t understand how to make it refresh without reloading the page.

  37. Mark MacLean on November 22, 2019 at 12:59 pm

    Neil, I can’t seem to get the icon to show up in my menu. If I paste the “” in my template, it shows up – but not where I want it to be. I really need it to be part of the menu. My menu id is “primary-menu”. Here is the code in the functions.php file:
    add_filter( ‘wp_nav_menu_primary-menu_items’, ‘woo_cart_but_icon’, 10, 2 ); // Change menu to suit – example uses ‘top-menu’

    /**
    * Add WooCommerce Cart Menu Item Shortcode to particular menu
    */
    function woo_cart_but_icon ( $items, $args ) {
    $items .= ‘[woo_cart_but]’; // Adding the created Icon via the shortcode already created

    return $items;
    }

    What am I doing wrong?

  38. Stephan on November 12, 2019 at 8:47 pm

    Thanks for the great code snippets. They are awesome.

    However, I needed to adjust the menu add_filter thus the Font Awesome Icon disappeared on the cart page all the time.

    add_filter( ‘wp_nav_menu_main-menu_items’, ‘woo_cart_but_icon’, 10, 2 ); // Change menu to suit – example uses ‘top-menu’

    /**
    * Add WooCommerce Cart Menu Item Shortcode to particular menu
    */
    function woo_cart_but_icon ( $items, $args ) {
    if (is_page(‘cart’)) {
    return $items;// just show the menu w/o the cart icon
    }else{
    $items .= do_shortcode(‘[woo_cart_but]’) ; // Adding the created Icon via the shortcode already created
    return $items;
    }
    }

    Maybe you have an idea why this happens. Once I refreshed the cart page and I saw the icon loading but disappearing after a second.

    Thanks a lot and keep up the good work.

  39. Christoph on October 21, 2019 at 6:42 am

    It works fine … but how can u add the total amount of price left to the right item numbers?

  40. STUDIOTOPO on September 15, 2019 at 11:19 am

    Hi,

    thank you, it works fine.

    But not working on the cart page: if I change the quantity and click ‘update cart’ button, the mini cart does not update…

    Any chance to fix this?

  41. Cristien on May 20, 2019 at 3:35 am

    Neil, can you tell me how to append the shortcode to the menu?
    I am a beginner and this step is vital for it to work correctly.

  42. Miguel on May 1, 2019 at 9:42 pm

    This is awesome thanks so much <3

  43. Erik Chavez on April 30, 2019 at 5:42 pm

    I got the shortcode to display the number of items in the cart, but the icon is missing. What am I doing wrong?

  44. Owen Fletcher on April 23, 2019 at 2:04 pm

    I found that the code on line 9 needed to be changed to “$items .= do_shortcode(‘[woo_cart_but]’) ;” & this generates the shortcode correctly.

  45. Sean on April 20, 2019 at 9:32 pm

    I did all the steps above but I am getting the [woo_cart_but] part only to show on the menu. There is no icon, and it isn’t even linking. I copied the three segments in to my template.php file.

    Is there additional code that is required?

    • Neil Gowran on April 20, 2019 at 11:51 pm

      The code functions need to be in functions.php the shortcode can be added in templates files/widgets/pagebuilders etc

  46. Chris Coffin on April 19, 2019 at 5:41 pm

    Where does the code snippet go?
    shortcodeoutput = do_shortcode( ‘[woo_cart_but]’ );
    items .= $shortcodeoutput; // Adding the created Icon via the shortcode already created

    When I place it my functions.php it breaks the site.

    • Neil Gowran on April 20, 2019 at 11:58 pm

      This would go in the template file not functions.php but not needed if you are using the add to menu method in the 3rd function.

  47. Neil Smart on March 28, 2019 at 2:13 pm

    Use this code to display/add shortcode to menu:

    $shortcodeoutput = do_shortcode( ‘[woo_cart_but]’ );
    $items .= $shortcodeoutput; // Adding the created Icon via the shortcode already created

  48. Scott Rod on March 14, 2019 at 7:41 pm

    This is displaying the shortcode as plain text as a menu item. In other words, it’s not returning a number of items even though there’s several items in the cart.

    • Cristien Lefrancois on May 25, 2019 at 9:56 pm

      I am also only getting plain text. The fontawesome icon is showing up but [woo_cart_but] is showing in plain text beside the icon in the menu.

      • Transcendant on September 23, 2019 at 7:41 pm

        I’ve added “do_shortcode” in the woo_cart_but_icon function and it worked for me. Like so:
        [code]
        function woo_cart_but_icon ( $items, $args ) {
        $items .= do_shortcode(‘[woo_cart_but]’); // Adding the created Icon via the shortcode already created

        [/code]

        • Josh on June 5, 2020 at 9:36 pm

          Thanks for the “do_shortcode()” bit!

Leave all Comment