1

How do we add custom sorts like Most View and Best Selling products in the API of Product List in Magento 2?

I need to add the most views and best-selling products in /V1/products to this API using search criteria.

eg: http://domain.com/index.php/rest/V1/products?searchCriteria[page_size]=10&searchCriteria[sortOrders][0][field]=most-view&searchCriteria[sortOrders][0][direction]=ASC

any help on this?

2 Answers 2

2

For custom sort filter through api, you need to create custom module,

Create app/code/Custom/Sort/etc/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\Api\ProductRepositoryInterface">
        <plugin name="custom_sort_plugin" type="Custom\Sort\Plugin\ProductRepository" sortOrder="10" disabled="false" />
    </type>
</config>

create the plugin file app/code/Custom/Sort/Plugin/ProductRepository.php

<?php 

namespace Custom\Sort\Plugin;
    
    class ProductRepository
    {
        public function afterGetList(
            \Magento\Catalog\Api\ProductRepositoryInterface $subject,
            \Magento\Catalog\Api\Data\ProductSearchResultsInterface $products
        ) {
            $searchCriteria = $products->getSearchCriteria();
            $sortOrders = $searchCriteria->getSortOrders();
            if ($sortOrders) {
                foreach ($sortOrders as $sortOrder) {
                    $field = $sortOrder->getField();
                    $direction = $sortOrder->getDirection();
    
                    if ($field === 'most_viewed') {
                        $collection->getSelect()->joinLeft(
                            ['report_views' => $collection->getTable('report_viewed_product_index')],
                            'e.entity_id = report_views.product_id',
                            ['views_count' => 'SUM(views_num)']
                        )->group('e.entity_id')->order('views_count ' . $direction);
                    } elseif ($field === 'best_selling') {
                        $collection->getSelect()->join(
                            ['sales_order_item' => $collection->getTable('sales_order_item')],
                            'e.entity_id = sales_order_item.product_id',
                            ['qty_ordered' => 'SUM(sales_order_item.qty_ordered)']
                        )->group('e.entity_id')->order('qty_ordered ' . $direction);
                    }
                }
            }
    
            return $products;
        }
    }

After this run the command

bin/magento cache:clean

bin/magento indexer:reindex

You should be now able to sort products in the /V1/products API using the "Most Viewed" and "Best Selling" sort options.

10
  • getting this error Class "Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor" not found. my magento version is 2.4.4-p1
    – Devidas
    Nov 6, 2023 at 12:03
  • can u comment on this
    – Devidas
    Nov 6, 2023 at 12:14
  • apologize, you can also use plugin as well, let me update answer Nov 6, 2023 at 12:16
  • updated the answer, can you try now Nov 6, 2023 at 12:25
  • Thank u . Let me try
    – Devidas
    Nov 6, 2023 at 12:27
0

try it like this i think reports and analytics requires auth

curl -X GET "https://your-magento-site.com/rest/V1/products?searchCriteria[sortOrders][0][field]=views&searchCriteria[sortOrders][0][direction]=DESC&searchCriteria[pageSize]=10" -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
2
  • this API works for the most viewed product ?
    – Devidas
    Nov 6, 2023 at 11:15
  • yes this OOTB, you can try it
    – Pramod
    Nov 6, 2023 at 15:37

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.