Prestashop 为特定类别创建自定义布局

Prestashop create custom layout for specific categories

我正在尝试为客户的某些类别创建不同的布局。我找到了一些解决方案,例如 here,但这是一个旧答案,我还需要更多详细信息。

我正在使用 Prestashop 1.6.0.9,我想创建一个自定义产品-list.tpl 文件来更改布局并添加一些小功能(获取产品描述、获取所有产品图片等)。

有没有人解决过这个问题?我不介意结构良好的漂亮代码,因为价格很低。如果使用硬编码既简单又快速,我会更喜欢它。

提前致谢。

解决方案可以有很多,但最常用的就是这些。

1。编辑主题文件(首选)

Prestashop 类别模板在文件 themes/[yourtheme]/category.tpl.

您可以像这样放置一些代码来编辑它:

YOUR_ID - 整数,类别的 ID。

{if isset($category) && isset($category->id) && $category->id == YOUR_ID}

    {* your custome code *}

{else}

    {* place all default category.tpl code here *}

{/if}

类别模板还包括 themes/[yourtheme]/product-list.tpl 以及您可以在代码中找到的其他文件。您可以相应地更改这些 .tpl 个文件。

2。覆盖类别控制器。

您可以覆盖类别控制器并使其使用任何其他模板文件,而不仅仅是 themes/[yourtheme]/category.tpl

2.1 使用如下代码创建文件 override/controllers/front/CategoryController.php

<?php

class CategoryController extends CategoryControllerCore
{
    public function initContent()
    {
        /* loading the default code */
        parent::initContent();

        /* please add all categories ID for which you want to use this custom template */
        $custom_categories = array(1, 3);

        if (isset($this->category) && in_array($this->category->id, $custom_categories))
        {
            /* please change the file name 'category-1.tpl' 
            to any other file name you want to use in your theme directory,
            i. e. themes/[yourtheme]/category-1.tpl */
            $this->setTemplate(_PS_THEME_DIR_.'category-1.tpl');
        }
    }
{
}

2.2 使用您的自定义代码创建文件 themes/[yourtheme]/category-1.tpl(或其他名称)。

2.3 可选。出于安全原因,将任何 index.php 文件从除根文件夹 之外的任何文件夹 复制到您在制作 2.1 部分时创建的文件夹(如果已创建)。

我也一直在寻找这个,但是为了 prestashop 1.7。 如果有帮助,这是我的解决方案。

覆盖 CategoryController 以便我们可以为每个类别使用不同的模板。 然后我们只需要创建一个类别为 friendly url name + category.tpl 的文件夹 如果文件不存在,则使用默认文件。

    <?php
class CategoryController extends CategoryControllerCore
{
    public function initContent()
    {

        parent::initContent();
        $this->setTemplate($this->getTpl());

        //$this->setTemplate('category.tpl');
        //$this->setTemplate('module:categoryController/views/templates/front/display.tpl');
    }
    protected function getTpl()
    {
      // it seems in prestashop 1.7, the search folder for setTemplate is myThemeFolder/templates/
      // we set the default layout if there's not custom files
      $layout = $this->template; // which here is   $this->template : 'catalog/listing/category.tpl'

      if ($parents = $this->category->getParentsCategories(Configuration::get('PS_LANG_DEFAULT')))
        {

            foreach ($parents as $parent)
            {
                $parent = (object) $parent;
                if (isset($parent->link_rewrite))
                {
                    // server full path for file_exists. Example : /var/www/vhosts/myvps.address.net/website.folder/themes/myThemeFolder/templates/layouts/categories/women/category.tpl
                    $categoryLayoutOverride_path = _PS_THEME_DIR_ . 'templates/layouts/categories/' . $parent->link_rewrite . '/category.tpl';

                    // for custom template on a particular category, we must have a folder with the friendly url name of the category + category.tpl, in myThemeFolder/templates/layouts/categories folder/
                    // then the layout path value must start at root of templates folder like this : layouts/categories/(category friendly url)/category.tpl
                    $categoryLayoutOverride = 'layouts/categories/' . $parent->link_rewrite . '/category.tpl';

                    if (file_exists($categoryLayoutOverride_path))
                    {
                        $layout = $categoryLayoutOverride;
                        break;
                    }
                }
            }
        }
        return $layout;
    }
}
?>