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;
    }
}
  • did you resolve this issue or still facing issue? could you please post your custom sort module in the question? – sandip Sep 28 '19 at 8:37
  • No, I have not resolved yet. I have updated my code, If you have any solution then please post it. – rnegi Sep 30 '19 at 5:11
  • is ur issue fixed? I am getting the same issue .. if ur issue fixed then please share an answer – Navin Bhudiya Jun 5 at 11:40
  • Have you find any solution for this issue? – Raj Sep 7 at 6:21
1

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

| improve this answer | |
0

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
| improve this answer | |

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.