PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 27, 2022

[FIXED] How to make a variable globally accessible in Prestashop

 October 27, 2022     prestashop, prestashop-1.5, prestashop-1.6, prestashop-1.7     No comments   

Issue

I'm modifying a module in Prestashop, which has folder structure like this..

css
translations
index.php
module.php
module.tpl

There is a variable $products that has an array of all the products. But it's only accessible to module.tpl which displays all the product on the homepage which I don't like.

I created a controller that redirects to a different page and a template/view/front/products.tpl to display all the products. But that $products variable is undefined in products.tpl file.


Solution

If you have created a new controller with a new template, you need to create in this controller this variable and assign this to a template, so:

In the initContent() function of the controller you need to create the variable "$products" with the values that you need, for example:

$products = Product::getProducts($id_lang, 0, 0, 'id_product', 'DESC' );

Then, you need to assign this php variable to a Smarty variable to show the value in the tpl file. To do this we use the method used by "@Ravinder Pal" but changing the value:

$this->context->smarty->assign('products', $products);

And finally you can use this variable in the template that is assigned in the initContent() function with this:

{$products}

Hope it helps you.



Answered By - Addis
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing