Skip to content

ProductRepositoryInterface plugin how to join another table with sort option?

I need to add the most-viewed and best-seller sort options into the rest/v1/products API. I created a plugin to add sort condition for following of below link

How to add custom sort like Most View and Best Selling products in API of Product List in Magento 2?

But in afterGetList plugin how to join a table to the collection. i inject MagentoCatalogModelResourceModelProductCollectionFactory but

below my code

<type name="MagentoCatalogApiProductRepositoryInterface">
        <plugin name="custom_sort_plugin" type="CustomSortPluginProductRepository" sortOrder="10" disabled="false" />
    </type>

ProductRepository plugin file

<?php 

namespace CustomSortPlugin;
    
    class ProductRepository
    {
        protected $productCollectionFactory;

        public function __construct(
        MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
        ) {
        $this->productCollectionFactory = $productCollectionFactory;
        }


        public function afterGetList(
            MagentoCatalogApiProductRepositoryInterface $subject,
            MagentoCatalogApiDataProductSearchResultsInterface $products
        ) {
            $searchCriteria = $products->getSearchCriteria();
            $sortOrders = $searchCriteria->getSortOrders();

            if ($sortOrders) {
                foreach ($sortOrders as $sortOrder) {
                    $field = $sortOrder->getField();
                    $direction = $sortOrder->getDirection();

                    if ($field === 'most_viewed') {
                        $collection = $this->productCollectionFactory->create();

                        $collection->addCategoryIds();

                        $collection->getSelect()->joinInner(
                            ['report_views' => $collection->getTable('report_event')],
                            'e.entity_id = report_views.object_id',
                            ['views_count' => 'COUNT(report_views.event_id)']
                        )->group('e.entity_id')->order('views_count ' . $direction);

                        //$collection->setPageSize(1); // Change the number to your desired limit
                        //$products->setItems($collection->getItems());
                        //$products->setTotalCount($collection->getSize());


                    } elseif ($field === 'best_selling') {
                        $collection = $this->productCollectionFactory->create();

                        $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);

                        //$products->setItems($collection->getItems());
                        //$products->setTotalCount($collection->getSize());
                    }
                }
            }
    
            return $products;
        }
    }

please help me with this what I am wrong? I am getting records but in response all data not coming.

best seller collection query print like this is the reason all data not coming to the response.

SELECT e.*, SUM(sales_order_item.qty_ordered) AS qty_ordered FROM
catalog_product_entity AS e INNER JOIN sales_order_item ON
e.entity_id = sales_order_item.product_id GROUP BY e.entity_id
ORDER BY qty_ordered DESC