Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a custom product "Sort by" field in prestashop?

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" )

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

I tried:

  • Editing the module files, this works, but I can't upgrade the module anymore if I want to keep the functionality.
  • Overriding, but can't seem to get it working, it still uses the same old module, not the overriden one.

So my questions are:

  1. How can you add the additional "Sort by" field in the products listing (front) in a most elegant/easiest way possible?I would love to hear for any other approaches to this problem.
  2. Can you do this without override/s, if you, for e.g, have bought another module that overrides the main module ( "Ps_facetedsearch", so that two overrides would not conflict)

Any tips are appreciated!!!

PrestaShop version: 1.7.4.2

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

}

Found in Ps_FacetedsearchProductSearchProvider.php (lines 117-136)

like image 802
Gintas Avatar asked Dec 30 '25 04:12

Gintas


1 Answers

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

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

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) 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) Make sure overrides is enabled in backend; from Advance Parameters > Performance

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) Check you shop; new custom sort order will be added.

Hope it helps!

like image 107
Divyesh Prajapati Avatar answered Jan 01 '26 19:01

Divyesh Prajapati