Get the number of items in cart in wordpress using woocommerce
Asked Answered
R

8

18

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

Rumery answered 17/3, 2017 at 20:28 Comment(1)
Hi, welcome to Stack Overflow! There is a typo in your question title. If you correct it people will be able to find and answer it a lot easier :)Mcburney
A
35

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

Allveta answered 18/3, 2017 at 0:43 Comment(0)
I
12

From your given description I understand that you want to show cart count only and nothing else. Here goes the code to do that

Add This code block In header.php or in that file where you want to display this widget...

<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php WC()->cart->get_cart_contents_count(); ?></a>

And Then Add this code block to functions.php

add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;

    ob_start();

    ?>
    <a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php WC()->cart->get_cart_contents_count(); ?></a>

    <?php

    $fragments['a.cart-customlocation'] = ob_get_clean();

    return $fragments;

}
Indiscriminate answered 18/3, 2017 at 10:49 Comment(4)
Just want to add a note that if you are testing this that cart fragments are cached, so if you make changes, clear your cache, delete the product from the cart and start again to see those changes - this tripped me up for an hour!Lean
I am using this, Its not working. I also tried replacing WC()->cart->get_cart_contents_count(); with ` $woocommerce->cart->get_cart_contents_count()` But its not working. some pointers.Aklog
very well explained, you shud have just added the woocommerce docs link. this was what i wantedBarrier
You forgot to "echo" the count: <a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo WC()->cart->get_cart_contents_count(); ?></a>Earmuff
F
7

Try This Code

global $woocommerce;
print_r(count(WC()->cart->get_cart()));
Freeland answered 3/5, 2019 at 5:51 Comment(3)
Your answer is working for me. Upvote from my side. I have to display the total number of products. I mean if I choose T-shirt with 2 qty and a bag with 3 qty so according to you your answer it will display the 2 output instead of 5. Thanks for the helpBanquette
Thank you mehul , as said by @NarenVerma I has the same requirement.Aurora
the global $woocommerce; line is not required here. you can remove that one and be perfeclty save. also try to explain what you are doing here. since it is not the answer of the OPs question. But it is still helping people, so maybe try to be more specific about the differences..Nonmetal
E
5

Further to Kodaloid's answer, you can also only show the cart count if it is greater than 0.

$cartcount = WC()->cart->get_cart_contents_count();
if ($cartcount > 0) { echo $cartcount; }
Erb answered 23/2, 2018 at 13:55 Comment(0)
H
3

From https://gist.github.com/mikejolley/2044101:

Add this code in your header.php file.

<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
Hanser answered 7/9, 2017 at 9:41 Comment(0)
H
3

$total_qty = WC()->cart->cart_contents_count;

H answered 27/5, 2021 at 6:19 Comment(0)
P
0

get_cart_contents_count() show the total quantity of all product items. i.e. you buy 2kg of product A and 1kg of product B, get_cart_contents_count() return 3.

If you need to show how many products are in cart, I did this in functions.php:

function count_item_in_cart() {
    $count = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $count++;
    }
    return $count;
}

then in your php file you can do this

<?php echo count_item_in_cart() . " products"; ?>
Premiere answered 4/7, 2018 at 13:53 Comment(1)
It would be nice know the reason why i get -2Premiere
S
0

the best way for get the number of items:

  1. Number of products >> echo count(WC()->cart->get_cart());
  2. Number of products and number of orders for each product : echo WC()->cart->get_cart_contents_count();
Spaceless answered 5/4, 2021 at 1:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.