如何在Prestashop

时间:2017-07-05 11:23:19

标签: prestashop prestashop-1.6 prestashop-1.5 prestashop-1.7

我正在修改Prestashop中的模块,它具有这样的文件夹结构..

css
translations
index.php
module.php
module.tpl

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

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

4 个答案:

答案 0 :(得分:1)

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

在控制器的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}

希望它对你有所帮助。

答案 1 :(得分:1)

我实际想出来了。我别无选择,只能实例化新类别,然后获取所有产品。 像这样:

$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));

答案 2 :(得分:0)

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

您的模块中的Ex:

      define('MYGLOBALVAR', 'data');

在你的模板中:

      $smarty.const.MYGLOBALVAR

答案 3 :(得分:0)

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

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

如果你想在tpl中打印,在控制器中你可以分配给smarty变量

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

并在tpl中使用它