VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: kiasati on February 07, 2018, 14:26:31 PM

Title: how to get number of products in cart
Post by: kiasati on February 07, 2018, 14:26:31 PM
Hi

I have a simple button at the the top of my site that points to the cart page.
I want to show the number of product that are in the cart.
whats the code for it?

I wanna put this code in template index.php file. so i want it to be clean and fast so it wont effect page load time even a little bit.

i appreciate any help...
Title: Re: how to get number of products in cart
Post by: Milbo on February 10, 2018, 22:17:51 PM
a kind of echo count($cart->products). You just need to check if you have $cart or $this->cart
Title: Re: how to get number of products in cart
Post by: Studio 42 on February 11, 2018, 01:36:31 AM
You can not dispay directly the product in cart in index.php.
You get this from Vm session or you need to initialise the cart(see mod_virtuemart_cart for the code)
Title: Re: how to get number of products in cart
Post by: bart198x on August 02, 2018, 22:57:21 PM
Hi.
Old topic i know, but...
vm3.2.15 J3.8.11
How could I get the amount of all products in component cart in overide of course?
I was trying to find something in $cart->products, but i can't find anything of which i could count. I know this can be done becouse cart module is doing it, but that is in $data->totalProductTxt and I stack on how to call it in component cart or calculate for my own in override component cart file. (scenario 2 products. first x 3 , second x5, and all i get was 2 as the products amount, without products quanatity. I need to get 8 :) )
Thanks for any advise or sugestion.
Title: Re: how to get number of products in cart
Post by: Studio 42 on August 03, 2018, 00:08:23 AM
Quote from: bart198x on August 02, 2018, 22:57:21 PM
Hi.
Old topic i know, but...
vm3.2.15 J3.8.11
How could I get the amount of all products in component cart in overide of course?
Do a var_dump($data)
You should see products array  and each have quantity
Title: Re: how to get number of products in cart
Post by: GJC Web Design on August 03, 2018, 00:12:27 AM
you need to get the cart by the means discussed above and loop thru the $cart products and accumulate the products*quantity to get the total
$total_prods = 0;
foreach ($this->cart->products as $product){
        $total_prods+= $product->quantity ;
}

these aren't the actual vars.. check the cart object
Title: Re: how to get number of products in cart
Post by: bart198x on August 03, 2018, 06:35:45 AM

GJC thanks a lot, that works :)