如何在 prestashop 中添加自定义产品“排序依据”字段?

Gin*_*tas 2 php model-view-controller prestashop symfony prestashop-1.7

我是 Prestashop 的新手,我正在尝试添加一个新的“排序依据”字段(默认情况下,您有:“相关性”,“名称,A 到 Z”,“名称,Z 到 A”,“价格,低至高”, “价格从高到低”)

如你们所知,该功能位于名为“Ps_facetedsearch”的模块中,链接此处

我试过:

  • 编辑模块文件,这可行,但如果我想保留功能,我无法再升级模块。
  • 覆盖,但似乎无法让它工作,它仍然使用相同的旧模块,而不是覆盖的模块。

所以我的问题是:

  1. 如何以最优雅/最简单的方式在产品列表(前面)中添加额外的“排序依据”字段?我很想听听解决此问题的任何其他方法。
  2. 例如,如果您购买了另一个覆盖主模块的模块(“Ps_facetedsearch”,这样两个覆盖就不会冲突),您可以在没有覆盖的情况下执行此操作吗?

任何提示表示赞赏!

PrestaShop 版本:1.7.4.2

我需要复制/粘贴 Ps_facetedsearch 模块中的行,以便添加额外的“排序依据”字段:

private function getAvailableSortOrders()
{
    return [
        (new SortOrder('product', 'position', 'asc'))->setLabel(
            $this->module->getTranslator()->trans('Relevance', array(), 'Modules.Facetedsearch.Shop')
        ),
        (new SortOrder('product', 'name', 'asc'))->setLabel(
            $this->module->getTranslator()->trans('Name, A to Z', array(), 'Shop.Theme.Catalog')
        ),
        (new SortOrder('product', 'name', 'desc'))->setLabel(
            $this->module->getTranslator()->trans('Name, Z to A', array(), 'Shop.Theme.Catalog')
        ),
        (new SortOrder('product', 'price', 'asc'))->setLabel(
            $this->module->getTranslator()->trans('Price, low to high', array(), 'Shop.Theme.Catalog')
        ),
        (new SortOrder('product', 'price', 'desc'))->setLabel(
            $this->module->getTranslator()->trans('Price, high to low', array(), 'Shop.Theme.Catalog')
        )
        // copy and paste here for another one, but lose the upgradability
       // of a module.
    ];

}
Run Code Online (Sandbox Code Playgroud)

位于 Ps_FacetedsearchProductSearchProvider.php(第 117-136 行)

小智 5

您可以通过覆盖模块添加自定义排序选项Ps_Facetedsearch

您可以按照以下步骤添加自定义排序。

ps_facetedsearch.php1)在文件夹中添加文件override/modules/ps_facetedsearch;(如果不存在则创建文件夹)以及此文件中的以下代码。

<?php
/**
 * @override Ps_Facetedsearch
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

require_once implode(DIRECTORY_SEPARATOR, array(
    __DIR__, 'src', 'Ps_FacetedsearchProductSearchProvider.php',
));

class Ps_FacetedsearchOverride extends Ps_Facetedsearch
{
    public function hookProductSearchProvider($params)
    {
        $query = $params['query'];
        // do something with query,
        // e.g. use $query->getIdCategory()
        // to choose a template for filters.
        // Query is an instance of:
        // PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery
        if ($query->getIdCategory()) {
            return new Ps_FacetedsearchProductSearchProviderOverride($this);
        } else {
            return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ps_FacetedsearchProductSearchProvider.php2)在文件夹中添加文件override/modules/ps_facetedsearch/src;(如果不存在则创建文件夹)并在其中添加以下代码。

<?php

require_once implode(DIRECTORY_SEPARATOR, array(
    __DIR__, '..', '..', '..', '..', 'modules', 'ps_facetedsearch', 'src', 'Ps_FacetedsearchProductSearchProvider.php',
));

require_once implode(DIRECTORY_SEPARATOR, array(
    __DIR__, '..', '..', '..', '..', 'modules', 'ps_facetedsearch', 'src', 'Ps_FacetedsearchFiltersConverter.php',
));

require_once implode(DIRECTORY_SEPARATOR, array(
    __DIR__, '..', '..', '..', '..', 'modules', 'ps_facetedsearch', 'src', 'Ps_FacetedsearchFacetsURLSerializer.php',
));

use PrestaShop\PrestaShop\Core\Product\Search\URLFragmentSerializer;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchProviderInterface;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchResult;
use PrestaShop\PrestaShop\Core\Product\Search\Facet;
use PrestaShop\PrestaShop\Core\Product\Search\FacetCollection;
use PrestaShop\PrestaShop\Core\Product\Search\Filter;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;

class Ps_FacetedsearchProductSearchProviderOverride extends Ps_FacetedsearchProductSearchProvider
{
    private $module;

    public function __construct(Ps_Facetedsearch $module)
    {
        $this->module = $module;
    }

    public function runQuery(
        ProductSearchContext $context,
        ProductSearchQuery $query
    ) {
        $facetedSearch = new Ps_FacetedsearchProductSearchProvider($this->module);
        $result = $facetedSearch->runQuery($context, $query);

        $sortOrders = $this->getAvailableSortOrders();
        foreach ($sortOrders as $sortOrder) {
            $result->addAvailableSortOrder($sortOrder);
        }

        return $result;
    }

    /**
     * New sort order that needs to be appended
     * 
     * @return array
     */
    private function getAvailableSortOrders()
    {
        return [
            // add your custom sort by orders here;
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

3) 确保overrides已在后台启用;从高级参数 > 性能

4)要加载您的覆盖,您需要re-index自动加载,并且为此您需要删除class_index.php文件;从中删除class_index.php文件var/cache/devvar/cache/prod文件夹。

5)检查您的商店;将添加新的自定义排序顺序。

希望能帮助到你!