如何添加自定义产品“排序依据"在prestashop中的字段? [英] How can I add a custom product "Sort by" field in prestashop?

查看:107
本文介绍了如何添加自定义产品“排序依据"在prestashop中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Prestashop的新手,我试图添加一个新的排序方式"字段(默认情况下,您具有:相关性",名称,从A到Z",名称,从Z到A",价格" ,从低到高",价格,从高到低")

I am new to Prestashop and I am trying to add a new "Sort by" field ( where by default you have: "Relevance" , "Name, A to Z" , "Name, Z to A", "Price, low to high", "Price, high to low" )

如您所知,该功能位于名为以下模块的模块中: "Ps_facetedsearch",链接此处.

As you guys know, the functionality is located in the module called: "Ps_facetedsearch" , link here.

我尝试过:

  • 编辑模块文件可以正常工作,但是如果我想保留功能,就无法升级该模块.
  • 覆盖,但似乎无法正常工作,它仍然使用相同的旧模块,而不是覆盖的模块.

所以我的问题是:

  1. 如何以最优雅/最简单的方式在产品列表(前面)中添加附加的排序依据"字段?我很想听听其他解决此问题的方法.
  2. 例如,如果您购买了另一个可替代主模块的模块("Ps_facetedsearch",以便两个替代不会冲突),您可以在没有替代的情况下做到这一点吗?

任何提示都值得赞赏!

PrestaShop版本: 1.7.4.2

PrestaShop version: 1.7.4.2

为了添加其他排序依据"字段,我需要复制/粘贴Ps_facetedsearch模块中的行:

The lines in the Ps_facetedsearch module that I need to copy/paste in order to add an additional "Sort by" field:

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.
    ];

}

在Ps_FacetedsearchProductSearchProvider.php中找到 (第117-136行)

Found in Ps_FacetedsearchProductSearchProvider.php (lines 117-136)

推荐答案

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

You can add custom sort by option by overriding Ps_Facetedsearch module.

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

You can follow below steps to add custom sort by order.

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

1) Add file ps_facetedsearch.php in folder override/modules/ps_facetedsearch; (create folders if not exists) and below code in this file.

<?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;
        }
    }
}

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

2) Add file Ps_FacetedsearchProductSearchProvider.php in folder override/modules/ps_facetedsearch/src; (create folders if not exists) and add below code in it.

<?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;
        ];
    }
}

3)确保在后端启用了overrides;来自 高级参数>性能

3) Make sure overrides is enabled in backend; from Advance Parameters > Performance

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

4) To load you overrides you need to re-index autoloads and to do so you need to delete class_index.php file; delete class_index.php file from var/cache/dev and var/cache/prod folders.

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

5) Check you shop; new custom sort order will be added.

希望有帮助!

这篇关于如何添加自定义产品“排序依据"在prestashop中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆