Get the number of items in cart in wordpress using woocommerce

Nemzytch picture Nemzytch · Mar 17, 2017 · Viewed 35.8k times · Source

i'm actually developping a website. But i'm facing an issue. I need to display the number of item that are in the cart but only the number, nothing else i dont want total amount or anything else. Juste the number of items.

I aim to display it over my " go to cart " link that is an image with an href. But this is not the main pb. The main pb is how to find a way to get only the number of item in the cart.

I'm using WordPress with Avada installed to be able to customize a bit more WP and get some includes features. But i'm not using the avada menu, I'm using a home made menu, and i want to display in it the number of item in cart.

And for the " shopping side " I'm using WooCommerce.

I saw many posts of hooks and everything about this but it was about " show number of items and total cart amount, i dont want to display total car amount, I just want the number. a bit like this way : Cart number of items: plein.com website

Answer

Kodaloid picture Kodaloid · Mar 18, 2017

Hey and welcome to the site, though I am new around here too! The WooCommerce documentation actually has a snippet for exactly this purpose:

https://docs.woocommerce.com/document/show-cart-contents-total/

In your case to just get the count, you could do something like this:

Cart Total: <?php echo WC()->cart->get_cart_contents_count(); ?>

Koda