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

Html Prestashop为特定类别创建自定义布局,html,css,smarty,prestashop,prestashop-1.6,Html,Css,Smarty,Prestashop,Prestashop 1.6,我正在尝试为客户端的某些类别创建不同的布局。我已经找到了一些解决方案,如,但这是一个古老的答案,我还需要一些更多的细节 我正在使用Prestashop 1.6.0.9,我想创建一个custom-product-list.tpl文件来更改布局并添加一些小功能(获取产品描述、获取所有产品图像等) 以前有人解决过这个问题吗?我不介意一个漂亮的结构良好的代码,因为它的价格很低。如果它是简单和快速的硬编码,我会喜欢它 提前感谢。有很多解决方案,但最常用的是这些 1.编辑主题文件(首选) Prestasho

我正在尝试为客户端的某些类别创建不同的布局。我已经找到了一些解决方案,如,但这是一个古老的答案,我还需要一些更多的细节

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

以前有人解决过这个问题吗?我不介意一个漂亮的结构良好的代码,因为它的价格很低。如果它是简单和快速的硬编码,我会喜欢它


提前感谢。

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

1.编辑主题文件(首选) Prestashop类别模板位于文件
themes/[yourtheme]/Category.tpl

您可以通过如下方式放置一些代码对其进行编辑:

<?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');
        }
    }
{
}
您的\u 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.覆盖类别控制器。 您可以覆盖Category控制器并使其使用任何其他模板文件,而不仅仅是
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');
        }
    }
{
}

除了prestashop 1.7,我也一直在寻找它。
这是我的解决方案,如果可以的话

重写CategoryController,以便我们可以为每个类别使用不同的模板。 然后我们只需要创建一个目录友好的url名称+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;
    }
}
?>

那么,您是尝试编写代码还是只需要在没有您参与的情况下完成工作?回答您已经提供的想法和代码,在您的情况下,如果您只需要替换product-list.tpl,请使用
{if$category->id==$your_id}。。。在category.tpl中包含自定义产品列表文件…{/if}
,非常感谢。这两种解决方案都有效,但我使用了第二个覆盖类别控制器。对我来说似乎更抽象。