Author Topic: how to get number of products in cart  (Read 477 times)

kiasati

  • Jr. Member
  • **
  • Posts: 102
    • JoomlaJoy
how to get number of products in cart
« 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...

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9674
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: how to get number of products in cart
« Reply #1 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
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 3402
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: how to get number of products in cart
« Reply #2 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)

bart198x

  • Beginner
  • *
  • Posts: 29
Re: how to get number of products in cart
« Reply #3 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.

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 3402
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
Re: how to get number of products in cart
« Reply #4 on: August 03, 2018, 00:08:23 am »
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

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 8203
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 2.6.22 & 3.2.14
Re: how to get number of products in cart
« Reply #5 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
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

bart198x

  • Beginner
  • *
  • Posts: 29
Re: how to get number of products in cart
« Reply #6 on: August 03, 2018, 06:35:45 am »

GJC thanks a lot, that works :)