This WooCommerce Cart Count Shortcode allows you to display the number of items in the WooCommerce cart anywhere on your WordPress site using a shortcode. This can be useful for creating custom cart displays or for adding cart information to your site’s header or footer.
Implementing the WooCommerce Cart Count Shortcode
To implement this functionality, you can use the following code snippet. Before you use it, ask yourself this:
How should the products in the cart be counted?
- Use
count( WC()->cart->get_cart() )
to get the amount of unique line items in the cart - Use
WC()->cart->get_cart_contents_count()
to get the sum of all quantities of all items in the cart
The code snippet below uses the first method.
/**
* WooCommerce Cart Count Shortcode
*
* @return void|int Returns void if WooCommerce plugin is not active. Returns the cart count otherwise.
*/
function wpsnippets_cart_count_shortcode() {
// Return if WooCommerce plugin is not active
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
// Get the amount of unique line items in cart
$cart_count = (int) count( WC()->cart->get_cart() );
// Return shortcode output, don't echo
return cart_count;
}
// Register the shortcode. Change the first parameter 'cart_count' to the shortcode you want
add_shortcode( 'cart_count', 'wpsnippets_cart_count_shortcode' );
This code defines a custom PHP function wpsnippets_cart_count_shortcode()
that checks if WooCommerce is active and returns the count of unique line items in the cart using the count( WC()->cart->get_cart() )
method. Remember to modify the snippet to use the other way of counting. If WooCommerce is not active, it returns void.
The add_shortcode()
function is then used to register the WooCommerce shortcode [cart_count]
with the wpsnippets_cart_count_shortcode()
function as a callback, to display the cart count.
To display the cart count anywhere on your site, simply use the [cart_count]
shortcode in your content or template files.
Examples
Example 1: Display the WooCommerce Cart Count in a Text Widget using a Shortcode
This use case demonstrates how to create a shortcode that displays the current cart count in a text widget. The shortcode can be placed in any text widget to dynamically show the number of items in the WooCommerce cart.
Use the code above to define a custom shortcode cart_count
that calls the wpsnippets_cart_count_shortcode
function. This function retrieves the cart count using the get_cart_contents_count
method of the WooCommerce cart object (WC()->cart
). The shortcode can be used in a text widget by simply adding [cart_count]
to the widget content.
Example usage:
<p>The cart currently contains [cart_count] items.</p>
For example, with 3 items in the cart this will output:
<p>The cart currently contains 3 items.</p>
Example 2: Display the Cart Count in a Menu Item
This use case demonstrates how to display the cart count in a menu item using a shortcode. By adding the shortcode to a custom menu item, the cart count will be dynamically updated and displayed within the menu.
function wpsnippets_add_cart_count_to_menu( $items, $args ) {
// Change 'primary' to desired menu location
if ( $args->theme_location == 'primary' ) {
// Add a menu item
$items .= '<li><a href="' . esc_url( wc_get_cart_url() ) . '">' . do_shortcode( '[cart_count]' ) . ' items in cart</a></li>';
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'wpsnippets_add_cart_count_to_menu', 10, 2 );
The code first defines the cart_count
shortcode as shown in the previous example. Then, it adds a filter to the wp_nav_menu_items
hook to modify the menu items. In the callback function, it checks if the menu location is ‘primary’ (change this to match your theme) and appends a new menu item with the cart count using the do_shortcode
function.
Example 3: Display the Cart Count in a Custom Template
This use case demonstrates how to display the cart count in a custom template file. By using the wpsnippets_cart_count_shortcode
function within the template, the cart count will be dynamically displayed wherever the template is used.
echo do_shortcode( '[cart_count]' );
Define the cart_count
shortcode as shown in the first example. To display the cart count in a custom template, simply add echo do_shortcode( '[cart_count]' );
within the template file. The cart count will be outputted wherever this line of code is placed.