{"id":1344,"date":"2024-07-05T15:19:08","date_gmt":"2024-07-05T12:19:08","guid":{"rendered":"https:\/\/plumrocket.com\/learn\/?p=1344"},"modified":"2024-10-10T11:08:52","modified_gmt":"2024-10-10T08:08:52","slug":"how-to-add-magento-2-sort-filter-by-rating","status":"publish","type":"post","link":"https:\/\/plumrocket.com\/learn\/magento-2-sort-filter-by-rating","title":{"rendered":"How to Add Magento 2 Sort &amp; Filter by Rating"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full disable_zoom\"><img loading=\"lazy\" width=\"1600\" height=\"600\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-sort-and-filter-by-rating.png\" alt=\"How to Add Magento 2 Sort and Filter by Rating\" class=\"wp-image-1526\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-sort-and-filter-by-rating.png 1600w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-sort-and-filter-by-rating-300x113.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-sort-and-filter-by-rating-1024x384.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-sort-and-filter-by-rating-768x288.png 768w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-sort-and-filter-by-rating-1536x576.png 1536w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-sort-and-filter-by-rating-1568x588.png 1568w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/figure>\n\n\n\n<p>It is essential for eCommerce stores to provide an effective sorting and filtering system, as it improves navigation and user experience on the website. Magento 2, by default, offers sorting only by Price, Position, and Product Name, but sometimes, this isn\u2019t enough.<\/p>\n\n\n\n<p>In this article, we will explore an option that can enhance the navigational functionality &#8211; sorting products by rating. This allows users to sort products based on the experiences of other buyers, which significantly increases confidence and speeds up the purchase decision.<\/p>\n\n\n\n<p>There are two approaches how to add Magento 2 sort by rating functionality:<\/p>\n\n\n\n<ol><li><em>Creating a custom plugin <\/em>(for developers). This approach enhances your default sorting functionality, but it involves coding and familiarity with Magento 2 development.<\/li><\/ol>\n\n\n\n<ol start=\"2\"><li><em>Using <\/em><a href=\"\/magento-layered-navigation\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Magento 2 Layered Navigation extension<\/em><\/a>.<\/li><\/ol>\n\n\n\n<p>Let\u2019s explore these approaches deeper.<\/p>\n\n\n\n<h2>Enable Magento 2 Sort by Rating with a Custom Plugin<\/h2>\n\n\n\n<p>To add the \u201cRating\u201d option to the default sorting functionality, we need to create a custom extension and plugins that modify its behavior. Follow the steps in this section to get everything done correctly.<\/p>\n\n\n\n<h3>Step 1 &#8211; Create a Magento 2 basic extension<\/h3>\n\n\n\n<p>This step explains how to set up the basic structure for your custom module. You need to create two files:<\/p>\n\n\n\n<ol><li><code>app\/code\/VendorName\/ModuleName\/etc\/module.xml<\/code> &#8211; This file defines the module name and any dependencies it has on other modules. Add the following code to the file:<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n     &lt;module name=\"VendorName_ModuleName\">\n         &lt;sequence>\n             &lt;module name=\"Magento_Catalog\"\/>\n         &lt;\/sequence>\n     &lt;\/module>\n&lt;\/config><\/code><\/pre><\/div>\n\n\n\n<ol start=\"2\"><li><code>app\/code\/VendorName\/ModuleName\/registration.php<\/code> &#8211; This file registers the module with Magento.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'VendorName_ModuleName',\n    __DIR__\n);<\/code><\/pre><\/div>\n\n\n\n<h3>Step 2 &#8211; Create a plugin for the class Magento\\Catalog\\Model\\Config<\/h3>\n\n\n\n<p>Plugins are a powerful way to extend the functionality of existing Magento classes without modifying the core code. In this step, you&#8217;re creating a plugin that modifies the get<br><code>AttributeUsedForSortByArray<\/code> method of the <code>Magento\\Catalog\\Model\\Config<\/code> class. In other words, this plugin displays a &#8220;Rating&#8221; option on the category page toolbar in Magento 2.<\/p>\n\n\n\n<ol><li>Declare a plugin in the <br><code>app\/code\/VendorName\/ModuleName\/etc\/frontend\/di.xml<\/code> file<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Catalog\\Model\\Config\">\n        &lt;plugin\n            name=\"vendor_catalog_config_plugin\"\n            type=\"VendorName\\ModuleName\\Plugin\\Catalog\\Model\\Config\" \/>\n    &lt;\/type>\n&lt;\/config><\/code><\/pre><\/div>\n\n\n\n<ol start=\"2\"><li>Create plugin class <br><code>app\/code\/VendorName\/ModuleName\/Plugin\/Catalog\/Model\/Config.php<\/code><\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace VendorName\\ModuleName\\Plugin\\Catalog\\Model;\n\nclass Config\n{\n    public function afterGetAttributeUsedForSortByArray(\\Magento\\Catalog\\Model\\Config $subject, $result)\n    {\n        $ratingField['rating'] = __(\"Rating\");\n        $result = array_merge($result, $ratingField);\n\n        return $result;\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"612\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-1.png\" alt=\"Enable Magento 2 Sort by Rating with a Custom Plugin \u2014 step 2\" class=\"wp-image-1350\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-1.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-1-300x152.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-1-1024x520.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-1-768x390.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h3>Step 3 &#8211; Create a plugin for the class Magento\\Catalog\\Model\\Config\\Source\\ListSort<\/h3>\n\n\n\n<p>Following the same logic, you need to create plugins for different classes to add the &#8220;Rating&#8221; option for sorting in different locations.<\/p>\n\n\n\n<p>In this step, let\u2019s create a plugin that displays a &#8220;Rating&#8221; option in the global configuration: Stores -&gt; Configuration -&gt; Catalog -&gt; Catalog -&gt; Storefront -&gt; Product Listing Sort by.<\/p>\n\n\n\n<ol><li>Declare plugin in the<br><code>app\/code\/VendorName\/ModuleName\/etc\/adminhtml\/di.xml<\/code> file.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Catalog\\Model\\Config\\Source\\ListSort\">\n        &lt;plugin\n            name=\"vendor_catalog_list_sort_plugin\"\n            type=\"VendorName\\ModuleName\\Plugin\\Catalog\\Model\\Config\\Source\\ListSort\" \/>\n    &lt;\/type>\n&lt;\/config><\/code><\/pre><\/div>\n\n\n\n<ol start=\"2\"><li>Create plugin class <br><code>app\/code\/VendorName\/ModuleName\/Plugin\/Catalog\/Model\/Config\/Source\/ListSort.php<\/code><\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace VendorName\\ModuleName\\Plugin\\Catalog\\Model\\Config\\Source;\n\nclass ListSort\n{\n    public function afterToOptionArray(\\Magento\\Catalog\\Model\\Config\\Source\\ListSort $subject, $result)\n    {\n        $result[] = ['label' => __(\"Rating\"), 'value' => \"rating\"];\n\n        return $result;\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"633\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4.png\" alt=\"Enable Magento 2 Sort by Rating with a Custom Plugin \u2014 step 3\" class=\"wp-image-1351\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4-300x157.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4-1024x537.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4-768x403.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h3>Step 4 &#8211; Create a plugin for the class Magento\\Catalog\\Model\\Category\\Attribute\\Source\\Sortby<\/h3>\n\n\n\n<p>Next, you need to create a plugin to display a &#8220;Rating&#8221; option on the category level: Catalog -&gt; Categories -&gt; Select the Category to configure -&gt; Display Settings -&gt; Default Product Listing Sort By.<\/p>\n\n\n\n<ol><li>Declare plugin in the<br><code>app\/code\/VendorName\/ModuleName\/etc\/adminhtml\/di.xml<\/code> file.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;type name=\"Magento\\Catalog\\Model\\Config\\Source\\ListSort\">\n        &lt;plugin\n            name=\"vendor_catalog_list_sort_plugin\"\n            type=\"VendorName\\ModuleName\\Plugin\\Catalog\\Model\\Config\\Source\\ListSort\" \/>\n    &lt;\/type>\n    &lt;type name=\"Magento\\Catalog\\Model\\Category\\Attribute\\Source\\Sortby\">\n        &lt;plugin\n            name=\"vendor_catalog_category_list_sort_plugin\"\n            type=\"VendorName\\ModuleName\\Plugin\\Catalog\\Model\\Category\\Attribute\\Source\\Sortby\" \/>\n    &lt;\/type>\n&lt;\/config><\/code><\/pre><\/div>\n\n\n\n<ol start=\"2\"><li>Create plugin class <code>app\/code\/VendorName\/ModuleName\/Plugin\/Catalog\/Model\/Category\/Attribute\/Source\/Sortby.php<\/code><\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace VendorName\\ModuleName\\Plugin\\Catalog\\Model\\Category\\Attribute\\Source;\n\nclass Sortby\n{\n    public function afterGetAllOptions(\\Magento\\Catalog\\Model\\Category\\Attribute\\Source\\Sortby $subject, $result)\n    {\n        $result[] = ['label' => __(\"Rating\"), 'value' => \"rating\"];\n\n        return $result;\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"558\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5.png\" alt=\"Enable Magento 2 Sort by Rating with a Custom Plugin \u2014 step 4\" class=\"wp-image-1353\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5-300x139.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5-1024x474.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5-768x355.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h3>Step 5 &#8211; Add a rating field to the Elasticsearch index<\/h3>\n\n\n\n<p>By default, Magento uses Elasticsearch for product search. In this step, we need to provide rating data for products being indexed in Elasticsearch and define a new &#8220;rating&#8221; field for the Elasticsearch index.<\/p>\n\n\n\n<ol><li>Create class <br><code>app\/code\/VendorName\/ModuleName\/Model\/Adapter\/BatchDataMapper\/RatingProvider.php<\/code> to add rating data to Elasticsearch.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace VendorName\\ModuleName\\Model\\Adapter\\BatchDataMapper;\n\nuse Magento\\AdvancedSearch\\Model\\Adapter\\DataMapper\\AdditionalFieldsProviderInterface;\nuse Magento\\Framework\\App\\ResourceConnection;\n\nclass RatingProvider implements AdditionalFieldsProviderInterface\n{\n    private $resourceConnection;\n\n    public function __construct(ResourceConnection $resourceConnection)\n    {\n        $this->resourceConnection = $resourceConnection;\n    }\n\n    public function getFields(array $productIds, $storeId)\n    {\n        $ratings = $this->getRatings($productIds, (int) $storeId);\n\n        $fields = [];\n\n        foreach ($productIds as $productId) {\n            $productRating = $ratings[$productId] ?? 0;\n            $fields[$productId] = [\"rating\" => $productRating];\n        }\n\n        return $fields;\n    }\n\n    private function getRatings($productIds, $storeId)\n    {\n        $connection = $this->resourceConnection->getConnection();\n        $select = $connection\n            ->select()\n            ->from(\n                ['main_table' => $connection->getTableName('review_entity_summary')],\n                ['entity_pk_value', 'rating_summary']\n            )\n            ->where('main_table.store_id = ?', $storeId)\n            ->order('entity_pk_value');\n\n         if ($productIds) {\n             $select->where('main_table.entity_pk_value IN(?)', $productIds);\n         }\n\n         return $connection->fetchPairs($select) ?: [];\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<ol start=\"2\"><li>Create class <br><code>app\/code\/VendorName\/ModuleName\/Model\/Elasticsearch\/Adapter\/FieldMapper<\/code> to add a rating field to the Elasticsearch index.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\nnamespace VendorName\\ModuleName\\Model\\Elasticsearch\\Adapter\\FieldMapper;\n\nuse Magento\\Elasticsearch\\Model\\Adapter\\FieldMapper\\Product\\FieldProvider\\FieldType\\ConverterInterface;\nuse Magento\\Elasticsearch\\Model\\Adapter\\FieldMapper\\Product\\FieldProviderInterface;\n\n\nclass RatingField implements FieldProviderInterface\n{\n    private $fieldTypeConverter;\n\n\n    public function __construct(\n        ConverterInterface $fieldTypeConverter\n    ) {\n        $this->fieldTypeConverter = $fieldTypeConverter;\n    }\n\n    public function getFields(array $context = []): array\n    {\n        return [\n            'rating' => [\n                'type' => $this->fieldTypeConverter->convert(ConverterInterface::INTERNAL_DATA_TYPE_INT)\n            ]\n        ];\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<ol start=\"3\"><li>Declare the new \u201cRating\u201d field in the <code>app\/code\/VendorName\/ModuleName\/etc\/di.xml<\/code> file for the Elasticsearch index.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;virtualType name=\"additionalFieldsProviderForElasticsearch\" type=\"Magento\\AdvancedSearch\\Model\\Adapter\\DataMapper\\AdditionalFieldsProvider\">\n       &lt;arguments>\n           &lt;argument name=\"fieldsProviders\" xsi:type=\"array\">\n               &lt;item name=\"rating\" xsi:type=\"object\">VendorName\\ModuleName\\Model\\Adapter\\BatchDataMapper\\RatingProvider&lt;\/item>\n           &lt;\/argument>\n       &lt;\/arguments>\n   &lt;\/virtualType>\n\n   &lt;type name=\"Magento\\Elasticsearch\\Model\\Adapter\\FieldMapper\\Product\\CompositeFieldProvider\">\n       &lt;arguments>\n           &lt;argument name=\"providers\" xsi:type=\"array\">\n               &lt;item name=\"rating_sort\" xsi:type=\"object\">VendorName\\ModuleName\\Model\\Elasticsearch\\Adapter\\FieldMapper\\RatingField&lt;\/item>\n           &lt;\/argument>\n       &lt;\/arguments>\n   &lt;\/type>\n&lt;\/config><\/code><\/pre><\/div>\n\n\n\n<h3>Step 6 &#8211; Create a plugin for the class Magento\\Catalog\\Block\\Product\\ProductList\\Toolbar<\/h3>\n\n\n\n<p>In this step, you will create a plugin to modify the product collection used for displaying product lists. It checks if the current sort order is &#8220;rating&#8221;. If yes, it joins the &#8220;review_entity_summary&#8221; table to the product collection and sorts by the &#8220;rating_summary&#8221; field.<\/p>\n\n\n\n<ol><li>Declare plugin in the <code>app\/code\/VendorName\/ModuleName\/etc\/di.xml<\/code> file.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;virtualType name=\"additionalFieldsProviderForElasticsearch\" type=\"Magento\\AdvancedSearch\\Model\\Adapter\\DataMapper\\AdditionalFieldsProvider\">\n       &lt;arguments>\n           &lt;argument name=\"fieldsProviders\" xsi:type=\"array\">\n               &lt;item name=\"rating\" xsi:type=\"object\">VendorName\\ModuleName\\Model\\Adapter\\BatchDataMapper\\RatingProvider&lt;\/item>\n           &lt;\/argument>\n       &lt;\/arguments>\n   &lt;\/virtualType>\n\n   &lt;type name=\"Magento\\Elasticsearch\\Model\\Adapter\\FieldMapper\\Product\\CompositeFieldProvider\">\n       &lt;arguments>\n           &lt;argument name=\"providers\" xsi:type=\"array\">\n               &lt;item name=\"rating_sort\" xsi:type=\"object\">VendorName\\ModuleName\\Model\\Elasticsearch\\Adapter\\FieldMapper\\RatingField&lt;\/item>\n           &lt;\/argument>\n       &lt;\/arguments>\n   &lt;\/type>\n   &lt;type name=\"Magento\\Catalog\\Block\\Product\\ProductList\\Toolbar\">\n       &lt;plugin name=\"vendor_catalog_toolbar_plugin\" type=\"VendorName\\ModuleName\\Plugin\\Catalog\\Block\\Product\\ProductList\\Toolbar\" \/>\n   &lt;\/type>\n&lt;\/config><\/code><\/pre><\/div>\n\n\n\n<ol start=\"2\"><li>Create plugin class <code>app\/code\/VendorName\/ModuleName\/Plugin\/Catalog\/Block\/Product\/ProductList\/Toolbar.php<\/code><\/li><\/ol>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php\n\nnamespace VendorName\\ModuleName\\Plugin\\Catalog\\Block\\Product\\ProductList;\n\nclass Toolbar\n{\n    public function aroundSetCollection(\n        \\Magento\\Catalog\\Block\\Product\\ProductList\\Toolbar $subject,\n        \\Closure $proceed,\n        $collection\n    ) {\n        $currentOrder = $subject->getCurrentOrder();\n        if ($currentOrder == \"rating\" ) {\n            $dir = $subject->getCurrentDirection();\n            $collection->getSelect()->joinLeft(\n                ['review_entity_summary'],\n                'e.entity_id = review_entity_summary.entity_pk_value AND review_entity_summary.store_id = \"' . $collection->getStoreId() . '\"',\n                [\n                    'rating_summary',\n                    'rating' => 'review_entity_summary.rating_summary'\n                ]\n            );\n            $collection->setOrder($currentOrder, $dir);\n        }\n\n        return $proceed($collection);\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<h3>Step 7 &#8211; Install the Extension and Index the New Rating Field in Elasticsearch<\/h3>\n\n\n\n<p>Run the following commands to install the extension, compile the code, deploy static content, and reindex the Elasticsearch index to include the new &#8220;Rating&#8221; field.<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">php bin\/magento setup:upgrade\nphp bin\/magento setup:di:compile\nphp bin\/magento setup:static-content:deploy\nphp bin\/magento indexer:reindex catalogsearch_fulltext<\/code><\/pre><\/div>\n\n\n\n<h2>How to Make \u201cRating\u201d the Default Sort Option in Magento 2<\/h2>\n\n\n\n<p>After you add the Magento 2 sort products by rating feature, you can set it as the default option for sorting the product list. To do this, go to the global configurations under Stores -&gt; Configuration -&gt; Catalog -&gt; Catalog -&gt; Storefront -&gt; Product Listing Sort by option.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"633\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4-1.png\" alt=\"How to Make \u201cRating\u201d the Default Sort Option \u2014 global configurations\" class=\"wp-image-1356\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4-1.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4-1-300x157.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4-1-1024x537.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-2-4-1-768x403.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>You can also configure the default sorting option at the category level. To do this, navigate to Catalog -&gt; Categories -&gt; select the required Category -&gt; Display Settings -&gt; Default Product Listing Sort By, and select \u201cRating\u201d.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"558\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5-1.png\" alt=\"How to Make \u201cRating\u201d the Default Sort Option \u2014 Catalog\" class=\"wp-image-1357\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5-1.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5-1-300x139.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5-1-1024x474.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-3-5-1-768x355.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h2>Enable Magento 2 Filter By Rating with Layered Navigation Extension<\/h2>\n\n\n\n<p>Another way to allow customers to sort products by rating in Magento 2 is through the Layered Navigation Extension. While it doesn\u2019t add the \u201cRating\u201d option directly to the sorting functionality, it enhances Magento 2 navigation filters by adding new ones, including \u201cRating\u201d. This greatly improves the default filtering functionality, allowing users to find the products they are looking for much faster.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"560\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-6.png\" alt=\"Enable Magento 2 Filter By Rating with Layered Navigation Extension \u2014 Frontend\" class=\"wp-image-1358\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-6.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-6-300x139.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-6-1024x475.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-6-768x357.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>To add the Magento 2 rating filter to your website, you need to follow these steps:<\/p>\n\n\n\n<div class=\"wp-block-group pr-notice pr-notice-info\"><div class=\"wp-block-group__inner-container\">\n<p class=\"pr-notice-title\">Step-by-step guide:<\/p>\n\n\n\n<ol><li><strong>Get and install the<\/strong> <a href=\"\/magento-layered-navigation\">Layered navigation extension<\/a>.<\/li><li><strong>Enable the module.<\/strong> Once the layered navigation is installed, you need to enable it and configure the basic settings. Refer to <a href=\"\/docs\/magento-layered-navigation\/v4\">user guides<\/a> to install and configure it properly.<\/li><li><strong>Include &#8220;Rating&#8221; in Layered Navigation.<\/strong> In the \u201cAttribute Filters\u201d section of the same page, you have to locate the \u201cRating\u201d filter in the \u201cAttributes Used in Layered Navigation\u201d column. That\u2019s it.<\/li><\/ol>\n<\/div><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"571\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-7.png\" alt=\"Enable Magento 2 Filter By Rating with Layered Navigation Extension \u2014 Backend\" class=\"wp-image-1359\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-7.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-7-300x142.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-7-1024x485.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/07\/add-rating-field-to-product-list-sorting-7-768x364.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>Magento 2 is a flexible platform that allows flexible customization and new functionalities to be added. In this tutorial, we have provided two ways to add Magento 2 sort by rating feature.<\/p>\n\n\n\n<p>The first option is to perform custom development to enhance the default Magento 2 sorting functionality with the new \u201crating\u201d option. By following the detailed steps and using code snippets we provide, you can add not only the \u201crating\u201d sorting option, but laso any others, such as \u201cNewset\u201d, \u201cBest Seller\u201d, \u201cMost Viewed&#8221;, etc.<\/p>\n\n\n\n<p class=\"has-dark-gray-color has-text-color\">Also, you can extend the functionality by changing the default sort direction from ascending to descending order. In this case, products with the highest ratings will be shown first. To do this, you need to modify the plugin in step 6 and set the sort direction to <code>DESC<\/code> instead of using the current direction <code>$dir<\/code>.<\/p>\n\n\n\n<p>The second option is to install the Layered Navigation extension to add the \u201cRating\u201d filter quickly and effortlessly. The extension not only allows users to filter products by rating, but also offers other essential filters that help users significantly narrow down their search results and save time, leading to higher conversion rates.<\/p>\n\n\n\n<p>With both custom development and the extension offering efficient solutions, you can empower your customers to sort and filter by ratings, ultimately increasing their satisfaction and boosting your store&#8217;s conversion rates. <\/p>\n\n\n\n<p>We hope you found this article useful. If you have any difficulties, we are happy to help you with our\u00a0<a href=\"\/magento-development\" target=\"_blank\" rel=\"noreferrer noopener\">Magento Development services<\/a>. Please comment and share. Thanks for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is essential for eCommerce stores to provide an effective sorting and filtering system, as it improves navigation and user experience on the website. Magento 2, by default, offers sorting only by Price, Position, and Product Name, but sometimes, this isn\u2019t enough.<\/p>\n<p>In this article, we will explore an option that can enhance the navigational functionality \u2013 sorting products by rating. This allows users to sort products based on the experiences of other buyers, which significantly increases confidence and speeds up the purchase decision.<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[121],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>2 Simple Ways to Add Magento 2 Sort &amp; Filter by Rating<\/title>\n<meta name=\"description\" content=\"Learn how to add sort by rating to your Magento 2 store: use our code snippets to customize the default functionality or use the ready-made extension.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/plumrocket.com\/learn\/magento-2-sort-filter-by-rating\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"2 Simple Ways to Add Magento 2 Sort &amp; Filter by Rating\" \/>\n<meta property=\"og:description\" content=\"Learn how to add sort by rating to your Magento 2 store: use our code snippets to customize the default functionality or use the ready-made extension.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/plumrocket.com\/learn\/magento-2-sort-filter-by-rating\" \/>\n<meta property=\"og:site_name\" content=\"Magento Tutorials for Beginners &amp; Professionals\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-05T12:19:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-10T08:08:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2024\/08\/magento-2-sort-and-filter-by-rating.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1344"}],"collection":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/comments?post=1344"}],"version-history":[{"count":37,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1344\/revisions"}],"predecessor-version":[{"id":1945,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/1344\/revisions\/1945"}],"wp:attachment":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/media?parent=1344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/categories?post=1344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/tags?post=1344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}