Jump to content

[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!

Link to comment
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 2
Link to comment
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)
Link to comment
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!

Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

Hello,

I am trying to do what you indicate in a Prestashop 1.7.5 (@Rolige), I have created a new module, but it does not work, I am not very clear where to place this line in the php.

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

This is my php file code for my module:

<?php
if(!defined('_PS_VERSION_'))
exit;

class extraInfo extends Module{
  
  public function __construct()
  {
      $this->name = 'extra_info';
      $this->tab = 'front_office_features';
      $this->version = '1.0.0';
      $this->author ='pShark';
      $this->need_instance = 0;
      $this->ps_versions_compliancy = array('min' => '1.7.x.x', 'max' => _PS_VERSION_);
      $this->bootstrap = true;

      parent::__construct();

      $this->displayName = $this->l('Product - Extra Information');
      $this->description = $this->l('Extra information in product page');
      $this->confirmUninstall = $this->l('Are you sure you want to uninstall the module?');

      $this->templateFile = 'module:extra_info/views/templates/hook/extra_info.tpl';
  }

  public function install()
  {
      return (parent::install()
      && $this->registerHook('displayHeader')
      && $this->registerHook('displayProductAdditionalInfo')
      );
  
      $this->emptyTemplatesCache();
  
      return (bool) $return;
  }

  public function hookDisplayHeader($params)
  {
    $this->context->smarty->assign('all_combinations', (new Product($id_product))->getAttributeCombinations());
    $this->context->controller->registerStylesheet('modules-extraInfo', 'modules/'.$this->name.'/views/css/extra_info.css', ['media' => 'all', 'priority' => 150]);
  }

  public function uninstall()
  {
    $this->_clearCache('*');

    if(!parent::uninstall() || !$this->unregisterHook('displayProductAdditionalInfo'))
      return false;

    return true;
  }

  public function hookDisplayProductAdditionalInfo()
  {
    return $this->display(__FILE__, 'views/templates/hook/extra_info.tpl');
  }

}

I have added the line to assign the smarty variable in hookDisplayHeader(), but I have tried to add it in hookDisplayProductAdditionalInfo() and __construct(), but it does not work.

Then in tpl file, I have placed the code that you indicated:

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

The module loads in the hook that I have indicated perfectly (I have tried to place a text in the tpl and it appears), but it does not load the information of the combinations, I imagine that the smarty variable is not assigned correctly.

 

Does anyone know why it doesn't work? do you see something wrong in my code?

 

Regards!

Edited by pShark (see edit history)
Link to comment
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
×
×
  • Create New...