Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 856
elPresta Avatar asked Oct 26 '25 09:10

elPresta


2 Answers

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']);
like image 131
PrestaShopDeveloper Avatar answered Oct 29 '25 07:10

PrestaShopDeveloper


In templates (Smarty) you can use:

{$product->quantity_all_versions}

{$product.quantity_all_versions}
like image 44
Limbic Avatar answered Oct 29 '25 06:10

Limbic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!