I have this lovely script that presents the user with the number of items in their cart but not the number of products...

 <?php echo sprintf (_n( '%d item', '%d',count( WC()->cart->get_cart())),count( WC()->cart->get_cart())); ?>

One issue that I am having is that when there is 1 item in cart it says "1 item", when there are more than 1 item it gives me simply a number "2".

How can I remove the word Item when there is only one item in the cart.

All help is appreciated.


1 Answer 1


You're passing two parameters to _n function, the first one is the pattern for one item, and the second for 2 or more. Just change the pattern.

Show only numbers:

<?php echo sprintf (_n( '%d', '%d',count( WC()->cart->get_cart())),count( WC()->cart->get_cart())); ?>

Show item for two or more:

<?php echo sprintf (_n( '%d', '%d items',count( WC()->cart->get_cart())),count( WC()->cart->get_cart())); ?>

For more information check here.

But, if you want to show only the number, you could echo the cart count directly:

echo count( WC()->cart->get_cart());


echo WC()->cart->get_cart_contents_count();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.