malo6 Posted March 29, 2022 Share Posted March 29, 2022 I want to display text next to the product photo depending on the carroers assigned to the product. I was partially able to do this on the product page. I modified controllers/front/ProductController.php by changing: $this->context->smarty->assign(array( 'pictures' => $pictures, 'textFields' => $text_fields, )); To: $this->context->smarty->assign(array( 'pictures' => $pictures, 'carriers' => $this->product->getCarriers(), 'textFields' => $text_fields, )); And then in themes/themeX/templates/catalog/product.tpl I can use following code: {if ($carriers[0]['id_carrier']==30)} Custom message {/if} The problem is that the same code doesn't work in product-list.tpl I will be grateful for your help. Link to comment Share on other sites More sharing options...
endriu107 Posted March 29, 2022 Share Posted March 29, 2022 Add in product-list.tpl this {debug} popup with available variables should appear. Link to comment Share on other sites More sharing options...
malo6 Posted March 29, 2022 Author Share Posted March 29, 2022 10 minutes ago, endriu107 said: Add in product-list.tpl this {debug} popup with available variables should appear. I have checked and there is no carrier information there. Link to comment Share on other sites More sharing options...
Ress Posted March 29, 2022 Share Posted March 29, 2022 In the product list, how do you send that variable? Because in the example given by you, if you send only in the product controller, it will be available only in the product page. You should use a hook, available in the template. Link to comment Share on other sites More sharing options...
malo6 Posted March 29, 2022 Author Share Posted March 29, 2022 7 minutes ago, Ress said: In the product list, how do you send that variable? Because in the example given by you, if you send only in the product controller, it will be available only in the product page. You should use a hook, available in the template. I send only in product controller. Any tips how to do it via hook? Link to comment Share on other sites More sharing options...
Ress Posted March 30, 2022 Share Posted March 30, 2022 You should do a function like this, in a module: public function hookDisplayProductPriceBlock($params) { // this check is necessary because the hook is executed several times, with different parameters (unit_price, weight) if ($params['type'] != 'before_price') { return false; } $product = $params['product']; $this->context->smarty->assign( array( 'custom_product_carriers' => $product->getCarriers(), ) ); // you will need to create this tpl and put the code for the front end in it (you will be able to use the $custom_product_carriers variable). return $this->display(__FILE__, 'views/templates/hook/product_carriers.tpl'); } And of course, don't forget to register the hook. 1 Link to comment Share on other sites More sharing options...
malo6 Posted March 30, 2022 Author Share Posted March 30, 2022 @Ress I'm getting an error: Fatal error: Uncaught Error: Call to a member function getCarriers() on array in Line: 'custom_product_carriers' => $product->getCarriers(), Link to comment Share on other sites More sharing options...
Ress Posted March 30, 2022 Share Posted March 30, 2022 public function hookDisplayProductPriceBlock($params) { // this check is necessary because the hook is executed several times, with different parameters (unit_price, weight) if ($params['type'] != 'before_price') { return false; } $product = new Product($params['product']->id); $this->context->smarty->assign( array( 'custom_product_carriers' => $product->getCarriers(), ) ); // you will need to create this tpl and put the code for the front end in it (you will be able to use the $custom_product_carriers variable). return $this->display(__FILE__, 'views/templates/hook/product_carriers.tpl'); } I updated. Link to comment Share on other sites More sharing options...
malo6 Posted March 30, 2022 Author Share Posted March 30, 2022 The error is gone, but custom_product_carriers variable is empty. product_carriers.tpl: {$custom_product_carriers|var_dump} Result: array(0) { } I have checked the products and they have shipping methods assigned to them. Link to comment Share on other sites More sharing options...
Ress Posted March 30, 2022 Share Posted March 30, 2022 (edited) I tested the code, and it showed me okay. Check the product id, make sure you give the var_dump for the right product. Edited March 30, 2022 by Ress (see edit history) 1 Link to comment Share on other sites More sharing options...
malo6 Posted March 30, 2022 Author Share Posted March 30, 2022 Finally got it! Just a little change. $product = new Product($params['product']['id_product']); Thank you very much for your help! 1 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