2

I'm trying to add custom sorting options like newest products,price high-to-low & price low-to-high in category page using plugin. After creating plugin it's working good in category page and search page but when i enable amasty's elastic search module then price high-to-low & price low-to-high sorting working proper in plp but not working in search page.

If from backend settings i'm select "MySql" option then custom sorting working proper and if i'm select elastic search from dropdown then it's not working.

enter image description here

Also newest product sorting not working in both plp and search page. I have already tried some of solutions suggested in stack as below links but no luck:-

Custom sort by is not working with elastic search in magento 2.3

Magento 2 How to add custom sort by option

Have somebody also faced same issue before? Any suggestions should be appreciated. Thanks.

Following are my module file:-

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="custom_custom_block_toolbar" type="Namespace\Modulename\Plugin\Catalog\Block\Toolbar" />
    </type>

    <type name="Magento\Catalog\Model\Config">
        <plugin name="custom_catalog_model_config" type="Namespace\Modulename\Plugin\Catalog\Model\Config" />
    </type>

</config>

Config.php

<?php

namespace Namespace\Modulename\Plugin\Catalog\Model;

class Config
{
    public function afterGetAttributeUsedForSortByArray(
    \Magento\Catalog\Model\Config $catalogConfig,
    $options,
    $requestInfo = null
    ) {
        unset($options['position']);
        $options['high_to_low'] = __('Price - High To Low');
        $options['low_to_high'] = __('Price - Low To High');
        $options['newest'] = __('Newest');
        return $options;

    }

}

Toolbar.php

<?php
namespace Namespace\Modulename\Plugin\Catalog\Block;

class Toolbar
{

    public function aroundSetCollection(
    \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
    \Closure $proceed,
    $collection
    ) {
    $currentOrder = $subject->getCurrentOrder();
    $result = $proceed($collection);

    if ($currentOrder) {
        if ($currentOrder == 'high_to_low') {
            $subject->getCollection()->setOrder('price', 'desc');
        } elseif ($currentOrder == 'low_to_high') {
            $subject->getCollection()->setOrder('price', 'asc');
        }
        elseif ($currentOrder == 'newest') {
            //$subject->getCollection()->setOrder('entity_id', 'desc');
            $subject->getCollection()->setOrder('created_at', 'desc');
        }
    }

    return $result;
    }

}
1
  • I've had the same issue and after a while i just created "dummy" attributes like "custom_date_added" using the backoffice and some code to automate these attributes. After adding those attributes I could use these attributes to sort by setting this up in the backoffice. – Arno Vande Cappelle Feb 16 at 7:39
0

For me in Magento 2.4.1 with preference low to high and high to low works but newest, I don't know why it's not working

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="[Vendor]\[Module]\Block\Product\ProductList\Toolbar"/>
    <type name="Magento\Catalog\Model\Config">
        <plugin name="custom_catalog_model_config" type="[Vendor]\[Module]\Plugin\Catalog\Model\Config"/>
    </type>
</config>

Config.php

<?php

namespace [Vendor]\[Module]\Plugin\Catalog\Model;

class Config
{
    public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
    {
        //Remove default sorting options
        unset($options['name']);
        unset($options['price']);

        //New sorting options
        $options['position'] = __('Recommended');
        $options['price_desc'] = __('Price High');
        $options['price_asc'] = __('Price Low');
        $options['newest'] = __('Newest First');

        return $options;
    }
}

Toolbar.php

    <?php
    
    namespace [Vendor]\[Module]\Block\Product\ProductList;
    
    class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
    {
    
        /**
         * Set collection to pager
         *
         * @param \Magento\Framework\Data\Collection $collection
         * @return \Magento\Catalog\Block\Product\ProductList\Toolbar
         */
        public function setCollection($collection)
        {
            $this->_collection = $collection;
    
            $this->_collection->setCurPage($this->getCurrentPage());
    
            // we need to set pagination only if passed value integer and more that 0
            $limit = (int)$this->getLimit();
            if ($limit) {
                $this->_collection->setPageSize($limit);
            }
            if ($this->getCurrentOrder()) {
                if (($this->getCurrentOrder()) == 'position') {
                    $this->_collection->addAttributeToSort(
                        $this->getCurrentOrder(),
                        $this->getCurrentDirection()
                    );
                } else {
                    if ($this->getCurrentOrder() == 'price_desc') {
                        $this->_collection->setOrder('price', 'desc');
                    } elseif ($this->getCurrentOrder() == 'price_asc') {
                        $this->_collection->setOrder('price', 'asc');
                    } elseif ($this->getCurrentOrder() == 'newest') {
                        $this->_collection->setOrder('e.created_at', 'desc');//NOT WORKING :-( 
                        //Please let me know if you find solution
                    }
    
                }
            }
            return $this;
        }
    
    }
8
  • Hi. Using preference also it's not working for me. you have tried with enabled elastic search? – Raj Dec 28 '20 at 4:45
  • In Magento 2.4.1 there are only elasticsearch options at configuration->catalog->catalog->catalog search->search engine = no MySQL options here only elasticsearch 7. imgur.com/YQ8de0b . And Yes my elasticsearch is on and I am able to sort products by low to high and high to low but not by newest – Klaus Mikaelson Dec 28 '20 at 5:30
  • yes you are right but your solutions is working after select elastic search from dropdown? – Raj Dec 28 '20 at 5:34
  • Yes elasticsearch 7 is selected for me. Which magento version you are working on? – Klaus Mikaelson Dec 28 '20 at 5:35
  • m2.3.4-p2. i'm using amasty's elastic search not magento's default elastic search. – Raj Dec 28 '20 at 5:48
0

For newest use $this->_collection->getSelect()->order('e.created_at DESC'); insted of $this->_collection->setOrder('e.created_at', 'desc');

It is working with Magento 2.4.X

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.