Cart icon in WooCommerce
Adding a cart icon to a menu, with the number of products in the cart loaded dynamically.

Contents
- Shortcode for printing the icon
- Filter to enable the cart count via Ajax
- Adding the cart icon to an existing menu
- Printing the icon
- CSS for the styling
Shortcode for printing the icon
Here is a way to add the WooCommerce cart icon to a WordPress menu.
The icon shows the cart item count, links to the cart page and displays the number of items that have been added to the cart.
Create a shortcode for the WooCommerce cart: it can be added to a menu, placed anywhere in the theme templates, or hooked into the theme's actions.
<?php // <~ don't add me in
/**
* Create Shortcode for WooCommerce Cart Menu Item
*/
function woo_cart_but() {
ob_start();
$cart_count = WC()->cart->cart_contents_count; // Set variable for cart item count
$cart_url = wc_get_cart_url(); // Set Cart URL
?>
<li><a class="menu-item cart-contents" href="<?php echo $cart_url; ?>" title="My Basket">
<?php
if ( $cart_count > 0 ) {
?>
<span class="cart-contents-count"><?php echo $cart_count; ?></span>
<?php
}
?>
</a></li>
<?php
return ob_get_clean();
}
add_shortcode ('woo_cart_but', 'woo_cart_but' );The code above produces the following shortcode, which you can use anywhere a shortcode is accepted:
[woo_cart_but]Filter to enable the cart count via Ajax
Add a filter that updates the cart count when it changes, using Ajax fragments.
<?php // <~ don't add me in
add_filter( 'woocommerce_add_to_cart_fragments', 'woo_cart_but_count' );
/**
* Add AJAX Shortcode when cart contents update
*/
function woo_cart_but_count( $fragments ) {
ob_start();
$cart_count = WC()->cart->cart_contents_count;
$cart_url = wc_get_cart_url();
?>
<a class="cart-contents menu-item" href="<?php echo $cart_url; ?>" title="<?php _e( 'View your shopping cart' ); ?>">
<?php
if ( $cart_count > 0 ) {
?>
<span class="cart-contents-count"><?php echo $cart_count; ?></span>
<?php
}
?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}Adding the cart icon to an existing menu
Add the cart icon to an existing menu.
The third snippet adds it to a menu using a variant of the wp_nav_menu_items filter. The variant lets you specify the slug of an existing menu in the filter name: for example, if the menu has the id 'top-menu', the filter name can be written as wp_nav_menu_top-menu_items.
If your menu had the ID primary-menu, the filter name would be written as wp_nav_menu_primary-menu_items.
<?php // <~ don't add me in
add_filter( 'wp_nav_menu_top-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;
}Printing the icon
You can also print the icon somewhere other than a menu, wherever you prefer.
<?php echo do_shortcode("[woo_cart_but]"); ?>CSS for the styling
Example CSS for adding the icon:
/* # WooCommerce cart icon CSS with FontAwesome 5
---------------------------------------------------- */
.cart-contents {
position: relative;
display: flex !important;
flex-flow: column nowrap;
justify-content: center;
}
.cart-contents:before {
font-family: 'Font Awesome 5 Free';
font-weight: 900;
content: "\f290" !important;
font-size: 30px;
color: #FF1493;
}
.cart-contents:hover {
text-decoration: none;
}
.cart-contents-count {
position: absolute;
top: 15px;
right: 1px;
transform: translateY(-105%) translateX(25%);
font-family: Arial, Helvetica, sans-serif;
font-weight: normal;
font-size: 12px;
line-height: 22px;
height: 22px;
width: 22px;
vertical-align: middle;
text-align: center;
color: #fff;
background: #000;
border-radius: 50%;
padding: 1px;
}This article is based on:
It would be more accurate to call it a translation of that article, but there you go.