Question

I'm using Prestashop 1.5 and created page with list of grouped products. I want to show quantity of each product in the cart. At shopping cart page exist $product.cart_quantity property, but on my page it doesn't. Please, explain me, how to show product quantity in the cart on my page.

Was it helpful?

Solution

Easyest way I can think of is to access the data via cookies since cart data is stored in them.

You can get cookies data like this:

    $context = Context::getContext();
    echo '<pre>',print_r($context->cookie, true).'</pre>';  

Prestashop Context is a registry for PHP variables that were previously accessed as globals. It aims to standardize the way these variables are accessed, and to make the code more robust by getting rid of global vars.

And our echo is just for example to show what info you can get from cookies.

When costumer adds something to the shopping-cart it automatically gives it a cart id (id_cart) and from there it's fairly easy to access to access that value to get all the info.

To get a cart id ( assuming you already got context ) use this

   $Cart = $context->cart;

This returns you a ID of a current cart.

Now you want to return the current products in the cart ( with all the information it includes ). For that you have to use the public function located in prestashop_main_folder/classes/cart.php

So to return all the current products just use the following line

    $Cart->getProducts($refresh = false, $id_product = false, $id_country = null)

And then it returns you a array with all the variables what you can easily then access.

BR's

OTHER TIPS

You can do the following:

$context=Context::getContext();
$id_cart=$context->cookie->id_cart;

if($id_cart=='') $id_cart=Tools::getValue('id_cart');

$theCart = new Cart($id_cart);   
$products = $theCart->getProducts(true);
$nbTotalProducts = 0;     

foreach ($products as $product)
{
  $nbTotalProducts += (int)$product['cart_quantity'];         
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top