Reading list Switch to dark mode

    How to customize default sorting option on product listing in PrestaShop 1.7

    Updated 17 October 2022

    In this blog, We are going to learn how we can customize existing sorting options in PrestaShop 1.7. First of all, we need to understand the concept of sorting.

    “Sorting is an algorithm which specifies the way to arrange data in a particular order OR you can say sorting is the process of arranging the data in ascending and descending order.”

    As we know PrestaShop provides some default sorting options with the installation. These sorting options manage by the PrestaShop default module named “ps_facetedsearch”.

    Sorting-Options
    Default Sorting Options

    You can add a custom sorting option in two ways.

    1) Directly by changing the code in ps_facetedsearch module. Here is file path below.

    Searching for an experienced
    Prestashop Company ?
    Find out More

    Path: modules/ps_facetedsearch/src/Product/SearchProvider.php

    2) We can manage things separately by creating a small module. We have created a demo module for this practical.

    Now we need to override the ps_facetedsearch module class & main file.

    Base_Path: modules/demomodule

    Firstly, We need to override the module main class on Base_Path/override/modules/ps_facetedsearch/ps_facetedsearch.php

    <?php 
    require_once (__DIR__.'/src/Product/SearchProvider.php'); 
    use PrestaShop\Module\FacetedSearch\URLSerializer; 
    use PrestaShop\Module\FacetedSearch\Filters\Converter; 
        
    class Ps_FacetedsearchOverride extends Ps_Facetedsearch 
    { 
        public function hookProductSearchProvider($params) 
        { 
            $query = $params['query']; 
            if ($query->getIdCategory()) { 
                $urlSerializer = new URLSerializer(); 
                return new SearchProviderOverride( 
                    $this, 
                    new Converter( 
                        $this->getContext(), 
                        $this->getDatabase(), 
                        $urlSerializer 
                    ), 
                    $urlSerializer 
                ); 
            } else { 
                return null; 
            } 
        } 
    }

    Now we need to modify available sorting options by overriding the existing SearchProvider.php class. Check below.

    Path: Base_Path/override/modules/ps_facetedsearch/src/Product/SearchProvider.php

    <?php 
    use PrestaShop\Module\FacetedSearch\Filters; 
    use PrestaShop\Module\FacetedSearch\URLSerializer; 
    use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext; 
    use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; 
    use PrestaShop\PrestaShop\Core\Product\Search\SortOrder; 
    use PrestaShop\Module\FacetedSearch\Product\SearchProvider; 
    class SearchProviderOverride extends SearchProvider 
    { 
        private $_module; 
        public function __construct( 
            Ps_Facetedsearch $module, 
            Filters\Converter $converter, 
            URLSerializer $serializer, 
            SearchFactory $searchFactory = null 
        ) { 
            parent::__construct($module, $converter, $serializer, $searchFactory); 
            $this->_module = $module; 
        }    
        public function runQuery( 
            ProductSearchContext $context, 
            ProductSearchQuery $query 
        ) { 
            $result = parent::runQuery($context, $query); 
            $result->setAvailableSortOrders($this->getCustomSorting()); 
            return $result; 
        } 
    
        private function getCustomSorting() 
        { 
            // Existing Sorting Options
            $sortSalesDesc = new SortOrder('product', 'sales', 'desc');
            $sortPosAsc = new SortOrder('product', 'position', 'asc');
            $sortNameAsc = new SortOrder('product', 'name', 'asc');
            $sortNameDesc = new SortOrder('product', 'name', 'desc');
            $sortPriceAsc = new SortOrder('product', 'price', 'asc');
            $sortPriceDesc = new SortOrder('product', 'price', 'desc');
            
            // Custom option added
            $sortDateDesc = new SortOrder('product', 'date_add', 'desc'); 
            $translator = $this->_module->getTranslator(); 
            return [ 
                $sortSalesDesc->setLabel(
                    $translator->trans('Best sellers', [], 'Modules.Facetedsearch.Shop')
                ),
                $sortPosAsc->setLabel(
                    $translator->trans('Relevance', [], 'Modules.Facetedsearch.Shop')
                ),
                $sortNameAsc->setLabel(
                    $translator->trans('Name, A to Z', [], 'Shop.Theme.Catalog')
                ),
                $sortNameDesc->setLabel(
                    $translator->trans('Name, Z to A', [], 'Shop.Theme.Catalog')
                ),
                $sortPriceAsc->setLabel(
                    $translator->trans('Price, low to high', [], 'Shop.Theme.Catalog')
                ),
                $sortPriceDesc->setLabel(
                    $translator->trans('Price, high to low', [], 'Shop.Theme.Catalog')
                ),
                $sortDateDesc->setLabel( 
                    $translator->trans('Product, Date add desc', [], 'Modules.Facetedsearch.Shop') 
                ), 
            ]; 
        } 
    }

    After these changes, you can see we have added a new sorting option in the sorting list.

    Option-Added

    Note: If you want to remove any existing sorting option then you just need to remove SortOrder & setLabel from the list.

    That’s all about this blog. Hope it will help you.

    If you are facing any issues or have any doubts about the above process, please feel free to contact us through the comment section.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected]

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Noel kennedy
    • Sunny Kumar (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home