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

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!

For 1.7.4.x

  • Go to /modules/ps_facetedsearch/src
  • Open Ps_FacetedsearchProductSearchProvider.php
  • Find private function getAvailableSortOrders() line
  • and add what you want like:

(new SortOrder('product', 'date_add', 'desc'))->setLabel( $this->module->getTranslator()->trans('Date added: latest first', array(), 'Modules.Facetedsearch.Shop') ), (new SortOrder('product', 'date_add', 'asc'))->setLabel( $this->module->getTranslator()->trans('Date added: earliest first', array(), 'Modules.Facetedsearch.Shop') ), (new SortOrder('product', 'position', 'asc'))->setLabel( $this->module->getTranslator()->trans('Relevance', array(), 'Modules.Facetedsearch.Shop') ),

for 1.7.6.x

You can find the file in /modules/ps_facetedsearch/src/Product/SearchProvider.php

and find:

private function getAvailableSortOrders()

for old versions: - themes/default/product-sort.tpl - add like:

<option value="date_add:desc" {if $orderby eq 'date_add' AND $orderway eq 'desc'}selected="selected"{/if}>{l s='Date added: latest first'}</option>