4

After upgrading magento 2.3 and enabling magento new feature elastic search 6, My custom module for sort by is not working. It is perfectly fine working with MySql option but not working with elastic search.

Below is my custom module code:

CS/SortBy/Plugin/Product/ProductList/Toolbar.php

<?php
namespace CS\SortBy\Plugin\Product\ProductList;

class Toolbar
{
    /**
    * Plugin
    *
    * @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
    * @param \Closure $proceed
    * @param \Magento\Framework\Data\Collection $collection
    * @return \Magento\Catalog\Block\Product\ProductList\Toolbar
    */
    public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $toolbar,
        \Closure $proceed,
        $collection
    ) {
        $this->_collection = $collection;
        $currentOrder = $toolbar->getCurrentOrder();
        $currentDirection = $toolbar->getCurrentDirection();
        $result = $proceed($collection);

        if ($currentOrder) {
            switch ($currentOrder) {

            case 'newest':
                $this->_collection
                    ->getSelect()
                    ->order('e.created_at DESC');
            break;

            case 'price_desc':
                $this->_collection
                    ->getSelect()
                    ->order('price_index.min_price DESC');
            break;

            case 'price_asc':
                $this->_collection
                    ->getSelect()
                    ->order('price_index.min_price ASC');
            break;

            default:        
                $this->_collection
                    ->setOrder($currentOrder, $currentDirection);
            break;

            }
        }
        return $result;
    }
}

CS/SortBy/Plugin/Model/Config.php

<?php
namespace CS\SortBy\Plugin\Model;

class Config
{
    /**
     * Adding custom options and changing labels
     *
     * @param \Magento\Catalog\Model\Config $catalogConfig
     * @param [] $options
     * @return []
     */
    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;
    }
}
4
  • did you resolve this issue or still facing issue? could you please post your custom sort module in the question?
    – sandip
    Commented Sep 28, 2019 at 8:37
  • No, I have not resolved yet. I have updated my code, If you have any solution then please post it.
    – rnegi
    Commented Sep 30, 2019 at 5:11
  • is ur issue fixed? I am getting the same issue .. if ur issue fixed then please share an answer Commented Jun 5, 2020 at 11:40
  • Have you find any solution for this issue?
    – Raj
    Commented Sep 7, 2020 at 6:21

2 Answers 2

3

you need to change used_for_sort_by to 1 for attribute 'created_at'.

Use setup/patch script to update the attribute value

$prodEntityTypeId = $installer->getEntityTypeId('catalog_product');
$installer->updateAttribute($prodEntityTypeId, 'created_at', 'used_for_sort_by', 1);

# optional if you want the value to come in frontend as Date
$installer->updateAttribute($prodEntityTypeId, 'created_at', 'frontend_label', 'Date');

use the below code to change the created_at functionality https://magento.stackexchange.com/a/149656/27183

1

Custom Sort with ElasticSearch in Magento issue can be fixed by following these simple steps:

  • Navigate to Stores->Attributes->Product in Admin.
  • Add New Attribute.
  • In Properties->Attribute Properties:
    • Add Default Label (Eg: Price: Low to High)
    • Select the Catalog Input Type for Store Owner (Eg: Price) (In other cases Text Field is enough).
  • In Properties->Advanced Attribute Properties:
    • Add Attribute Code (Eg: price_asc).
  • In Storefront Properties->Storefront Properties:
    • Select User for Sorting in Product Listing to Yes

Once done with the above steps run the following commands,

bin/magento indexer:reindex catalogsearch_fulltext

bin/magento indexer:reindex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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