elPresta
elPresta

Reputation: 648

prestashop get total quantity

I would like to output my products quantity, but if product has combination I can`t get quantity.

Im using: $product[ 'quantity' ] to get product quantity. It works if I have product without combinations.

And its strange that in BO if I open products tab I can see total quantity: 20 (10 qnt red comb, 10 qnt black comb). So it would be fine for me. But when im exporting data i get quantity: 0 because main product quantity: 0 and product has 10 red qnt and 10 black qnt combination. Im able to get attributes list, but how to get combination list with quantities or just total quantity?

Upvotes: 4

Views: 6416

Answers (3)

Damian
Damian

Reputation: 1

To get quantity with Sizes I use Advanced -> SQL Manager

SELECT DISTINCT CONCAT(ps_product_lang.id_product) AS ID, CONCAT(ps_product_lang.name) AS Nazwa, CONCAT(ps_attribute_lang.name) AS Rozmiar,  CONCAT(ROUND(ps_product_shop.price*1.23,2)) AS Cena, CONCAT(ps_stock_available.quantity) AS Ilość, CONCAT('http://pasujemito.pl/',ps_category_lang.link_rewrite,'/',ps_product_lang.id_product,'-',ps_product_lang.link_rewrite,'.html') AS Link
FROM  ps_stock_available INNER JOIN ps_product_lang
USING (id_product) INNER JOIN ps_product_attribute_combination
USING (id_product_attribute) INNER JOIN ps_attribute_lang
USING (id_attribute) INNER JOIN ps_product_shop
USING (id_product) INNER JOIN ps_category_product
USING (id_product) INNER JOIN ps_category_lang
USING (id_category)
WHERE quantity=0 AND id_category NOT IN (1 ,2, 44, 45, 46, 75)  AND (id_product_attribute!=0)
ORDER BY Nazwa;

or if I want to see image link:

SELECT DISTINCT CONCAT(ps_product_lang.id_product) AS ID, CONCAT(ps_product_lang.name) AS Nazwa, CONCAT(ps_attribute_lang.name) AS Rozmiar,  CONCAT(ROUND(ps_product_shop.price*1.23,2)) AS Cena, CONCAT(ps_stock_available.quantity) AS Ilość, CONCAT('http://pasujemito.pl/',ps_image.id_image,'-tm_thickbox_default/',ps_product_lang.link_rewrite,'.jpg') AS Zdjęcie
FROM  ps_stock_available INNER JOIN ps_product_lang
USING (id_product) INNER JOIN ps_product_attribute_combination
USING (id_product_attribute) INNER JOIN ps_attribute_lang
USING (id_attribute) INNER JOIN ps_product_shop
USING (id_product) INNER JOIN ps_image
USING (id_product)
WHERE quantity=0 AND (ps_image.position=1) AND (id_product_attribute!=0)
ORDER BY Nazwa;

Upvotes: 0

Limbic
Limbic

Reputation: 51

In templates (Smarty) you can use:

{$product->quantity_all_versions}

{$product.quantity_all_versions}

Upvotes: 4

PrestaShopDeveloper
PrestaShopDeveloper

Reputation: 3118

If you need it at the Front Office template, you need to call is as object property:

{$product->quantity}

This will return the total quantity of all combinations.

If you need it at the controller, it's the same:

$product = new Product($id_product);
echo $product->quantity;

This will return again the total quantity of all combinations.

Regarding the database: The quantities are being kept at the: ps_stock_available table. If you need to get the total quantity of a product including combinations, get the "quantity" field where "id_product_attribute" = 0.

Here's a sample query for product with ID=1:

SELECT p.id_product, sa.quantity
FROM ps_product p 
INNER JOIN ps_stock_available sa ON p.id_product = sa.id_product AND id_product_attribute = 0
WHERE p.id_product = 1

If you loop through products from $products = Product::getProducts(...)

foreach ($products as $product)
    echo Product::getQuantity($product['id_product']);

Upvotes: 4

Related Questions