Jump to content
User17745

[PrestaShop 1.7] How to get stock quantities for all the different combinations of a product?

Recommended Posts

Hi,

I want to be able to display a table on the product page, the table will display the available stock quantities of all the different combinations of the product on PrestaShop 1.7.4

eg: If the product has two attributes: Color{White, Black} and Size{Small, Medium} then I should be able to get the quantities for the following pairs:

  • White/Small
  • White/Medium
  • Black/Small
  • Black/Medium

 

I am able to get the quantity of the currently selected product on the product page by using {$product.quantity} but it only gives me just that. How do I access all of the quantities?

 

Any help would be appreciated.

Thanks!

Share this post


Link to post
Share on other sites

Hello,

That's because the stock is obtained by “Ajax” on every change of the combination and stock, if you need the full stock or details of all combinations you should send to the tpl that variable.

In your php, by a module for example, you can send this variable to Smarty:

$this->context->smarty->assign('all_combinations', (new Product($id_product))->getAttributeCombinations());

And then you can use it in the tpl:

{foreach from=$all_combinations item=item}
    {$item.quantity}
{/foreach}

Each array contain all this information:

"id_product_attribute" => "1"
"id_product" => "1"
"reference" => "demo_1"
"supplier_reference" => ""
"location" => ""
"ean13" => ""
"isbn" => ""
"upc" => ""
"wholesale_price" => "0.000000"
"price" => "0.000000"
"ecotax" => "0.000000"
"quantity" => 299
"weight" => "0.000000"
"unit_price_impact" => "0.000000"
"default_on" => "1"
"minimal_quantity" => "1"
"low_stock_threshold" => null
"low_stock_alert" => "0"
"available_date" => "0000-00-00"
"id_shop" => "1"
"id_attribute_group" => "1"
"is_color_group" => "0"
"group_name" => "Tamaño"
"attribute_name" => "S"
"id_attribute" => "1"

 

Regards!

  • Like 1

Share this post


Link to post
Share on other sites

@Rolige

Great! It worked nicely after I created a module and used your code with some changes to it.

I now get an array of objects with each object corresponding to a single variation (size and color separate) which are associated to each other with the help of common $id_product_attribute and $attribute_name.

Do I have to manual come up with an algorithm to find the groups (like White/Small = 10 qty, White/Black = 15 qty, etc.) or are their any in built methods that I may be able to use directly?

 

Edit:

Is there a way to also get the corresponding images?

Edited by User17745 (see edit history)

Share this post


Link to post
Share on other sites

There are other methods you can try in the Product class, eg, there is one to get the summarized attributes, the method is called getAttributesResume() and return information like this:

"id_product_attribute" => "1"
"id_product" => "1"
"reference" => "demo_1"
"supplier_reference" => ""
"location" => ""
"ean13" => ""
"isbn" => ""
"upc" => ""
"wholesale_price" => "0.000000"
"price" => "0.000000"
"ecotax" => "0.000000"
"quantity" => 300
"weight" => "0.000000"
"unit_price_impact" => "0.000000"
"default_on" => "1"
"minimal_quantity" => "1"
"low_stock_threshold" => null
"low_stock_alert" => "0"
"available_date" => "0000-00-00"
"id_shop" => "1"
"attribute_designation" => "Size - S, Color - White"

As you can see there is a index (attribute_designation) which contain similar information to the one you need, and the separator can be changed when you call this method.

 

Regards!

Share this post


Link to post
Share on other sites

@Rolige

This method works well for my intended use but is there also a way to get the associated product image for the combination? 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More