-1

I want to make a custom filter for Most Viewed Product on Category Page in Magento 2.

I have used this code app/code/Hlcategory/Filter/Plugin/Catalog/Block/Toolbar.php

public function afterSetCollection(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
    $result,
    $collection
) {
    $this->_collection = $collection;

    if ($subject->getCurrentOrder() == 'most_viewed') {
        if (!$this->_subQueryApplied) {
            $reportEventTable = $this->_collection->getResource()->getTable('report_event');
            $subSelect = $this->_conn->select()->from(
                ['report_event_table' => $reportEventTable],
                'COUNT(report_event_table.event_id)'
            )->where(
                'report_event_table.object_id = e.entity_id'
            );

            $this->_collection->getSelect()->reset(Select::ORDER)->columns(
                ['views' => $subSelect]
            )->order(
                'views '  . self::SORT_ORDER_DESC
            );

            $this->_subQueryApplied = true;
        }
    }

    return $this;
}

I have checked but it doesn't work. It just shoes product like initial wise ( by product added date )

I have checked

$collection = $this->_productsFactory->create()->addAttributeToSelect(
        '*'
    )->addViewsCount()->setStoreId(
        $storeId
    )->addStoreFilter(
        $storeId
    );

It also shows empty array. any idea regarding this how to make that filter working.

and is there any key regarding How to know which product is viewed more from Admin side or frontend.

Any help is appriciated.

0

By default, Magento 2 shows the most viewed product in Magento Admin > Dashboard > choose tab Most Viewed Products. The _prepareCollection() method in vendor/magento/module-backend/Block/Dashboard/Tab/Products/Viewed.php is used for selecting products.

you can use that same code. You can added category condition here if you want.

class Index extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
     */
    protected $_productsFactory;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
        array $data = []
    ) {
        $this->_productsFactory = $productsFactory;
        parent::__construct($context, $data);
    }

    /**
     * Getting most viewed products
     */
    public function getCollection()
    {

        $currentStoreId = $this->_storeManager->getStore()->getId();

        $collection = $this->_productsFactory->create()
        ->addAttributeToSelect(
            '*'
        )->addViewsCount()->setStoreId(
                $currentStoreId
        )->addStoreFilter(
                $currentStoreId
        );
        $items = $collection->getItems();
        return $items;
    }
}
5
  • my most viewed products is empty it shows We couldn't find any records. any idea why is it empty ? – Prits Jun 1 at 4:53
  • Have you check into the admin, does it show data there first? – Dhiren Vasoya Jun 1 at 4:56
  • No really actually i have just installed from around 10 days and work around in it as i have checked it now only as i required this now so checked now but it doesn't show data. – Prits Jun 1 at 5:01
  • I think first you need to added some product and then open the same product from the different location, then it will show you the data. – Dhiren Vasoya Jun 1 at 5:18
  • Thanks for your response then my filter for most viewed product which i have written in toolbar.php it is in working like my code is correct. – Prits Jun 1 at 5:29

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.