Add Custom Attribute to Sort option...

VPS

Requirement

In our webshop, we have products with a discontinued flag, and a custom attribute added to the product section of Magento to mark a product. When a customer searches for products, the result will also include these discontinued products. We do not plan to hide these results, but it’s quite annoying it comes in the first positions. So we need a solution to move all these products as last results.

In Magento 2.4 and later uses the elastic search for the searching, we need to modify the sort array responsible for that.

To override the getSort() method in Magento\Elasticsearch\SearchAdapter\Query\Builder\Sort using an after plugin in Magento 2, you can follow these steps.

1. Create a Custom Module

If you don’t have a custom module, you need to create one.

Example Directory Structure:

app/code/Vendor/Module/
├── registration.php
├── etc
│   ├── module.xml
│   └── di.xml

2. Define registration.php

In app/code/Vendor/Module/registration.php, register your module:

3. Define module.xml

In app/code/Vendor/Module/etc/module.xml, declare your module:

4. Define the Plugin in di.xml



    

Now, define your plugin in the di.xml file to hook into the getSort() method of Magento\Elasticsearch\SearchAdapter\Query\Builder\Sort.

In app/code/Vendor/Module/etc/di.xml:




    
        
    

5. Create the Plugin Class

Now create the plugin class in app/code/Vendor/Module/Plugin/Elasticsearch/SearchAdapter/Query/Builder/SortPlugin.php.

 'asc'];
        
        return $result;
    }
}

Explanation:

  • afterGetSort(): This method runs after the original getSort() method. The second parameter ($result) is the result of getSort().
  • You can modify the $result array by adding custom sort logic or fields. In this example, we are adding a custom sort on a field named discontinued.

6. Run Commands

After adding the plugin, run the following Magento commands to update the module and clear caches:

php bin/magento setup:di:compile
php bin/magento cache:flush

This will ensure that your plugin is properly registered and compiled.

Result:

The after plugin will modify the result of getSort() in Magento\Elasticsearch\SearchAdapter\Query\Builder\Sort after the original method has been executed. You can now append custom sort logic or alter the returned sorting criteria.

You can add as many attributes to this array, but make sure it will not break the logic.

Did this post help you?

Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:

  • Just want to thank us? Buy us a Coffee
  • May be another day? Shop on Amazon using our links.
    Your prices won’t change but we get a small commission.


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

Leave a Comment