User17745 Posted August 21, 2018 Share Posted August 21, 2018 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 More sharing options...
Rolige Posted August 21, 2018 Share Posted August 21, 2018 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! 2 Link to comment Share on other sites More sharing options...
User17745 Posted August 22, 2018 Author Share Posted August 22, 2018 (edited) @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 August 22, 2018 by User17745 (see edit history) Link to comment Share on other sites More sharing options...
Rolige Posted August 22, 2018 Share Posted August 22, 2018 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 More sharing options...
User17745 Posted August 25, 2018 Author Share Posted August 25, 2018 @Rolige This method works well for my intended use but is there also a way to get the associated product image for the combination? Link to comment Share on other sites More sharing options...
TG Team Posted August 19, 2020 Share Posted August 19, 2020 On 8/21/2018 at 5:44 PM, Rolige said: $this->context->smarty->assign('all_combinations', (new Product($id_product))->getAttributeCombinations()); Hello, Please which php file where i will put this code? Link to comment Share on other sites More sharing options...
elburgl69 Posted August 21, 2020 Share Posted August 21, 2020 On 8/19/2020 at 10:46 AM, TG Team said: Hello, Please which php file where i will put this code? Normally in the hookHookName method in the main class of your module. Link to comment Share on other sites More sharing options...
pShark Posted October 28, 2021 Share Posted October 28, 2021 (edited) 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 October 28, 2021 by pShark (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now