如何在 Prestashop 中使变量全局可访问



我正在修改Prestashop中的一个模块,该模块具有这样的文件夹结构。

css
translations
index.php
module.php
module.tpl

有一个变量$products包含所有产品的数组。但它只能通过 module.tpl 访问,它在主页上显示我不喜欢的所有产品。

我创建了一个重定向到不同页面的控制器和一个模板/视图/正面/产品.tpl来显示所有产品。但是$products变量在 products.tpl 文件中未定义。

如果您使用新模板创建了一个新控制器,则需要在此控制器中创建此变量并将其分配给模板,因此:

在控制器的initContent()函数中,您需要使用所需的值创建变量"$products",例如:

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

然后,您需要将此 php 变量分配给 Smarty 变量以在 tpl 文件中显示该值。为此,我们使用"@Ravinder Pal"使用的方法,但更改值:

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

最后,您可以在 initContent() 函数中分配的模板中使用此变量:

{$products}

希望对您有所帮助。

我实际上想通了。我别无选择,只能实例化新类别,然后获取所有产品。 喜欢这个:

$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
$nb = 10000;    
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));

定义常量可以完成这项工作,也可以使用会话。之后,所有内容都可以通过智能模板访问。

例如在您的模块中:

define('MYGLOBALVAR', 'data');

在您的模板中:

$smarty.const.MYGLOBALVAR

您可以设置Cookie变量并在您想要的地方使用它。 如 :

$this->context->cookie->products=$products;

如果要在TPL中打印,则可以在控制器中分配给智能变量

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

并在 TPL 中使用它

最新更新