i want recently viewed widget to display bestsellers if there is no recently view products. I tried to create my own recently viewed widget, but all answers and examples use deprecated class:


So I wanted to copy standard widget and change it, but in Magento widget looks like this:

namespace Magento\Catalog\Block\Widget;

use Magento\Ui\Block\Wrapper;

 * Dynamically creates recently viewed widget ui component, using information
 * from widget instance and Catalog/widget.xml
class RecentlyViewed extends Wrapper implements \Magento\Widget\Block\BlockInterface

And I dont know how to reach and check returned collection or override this functionality.

1 Answer 1


The block Magento\Reports\Block\Product\Viewed has been deprecated. That way just possible on magento version 2.3 and earlier but now it has been replace and switch to use the new entity model to reserve data for tracking frontend user action in 2.4.5 version.

According to what I found from the Catalog module in core. Now they use the new entity model : 'Magento\Catalog\Model\ProductFrontendAction' for storing 'recently_viewed' & 'recently_compared' records.

Magento use the class 'Magento\Catalog\Model\Product\ProductFrontendAction\Synchronizer' to load the collection of frontend action entity for each action type.

So what you need is create a collection, then load and filtering item by the current customer_id or visitor_id which can get via the session manager.

Refer to below code:

use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\ResourceModel\ProductFrontendAction\Collection;
use Magento\Catalog\Model\ResourceModel\ProductFrontendAction\CollectionFactory;
use Magento\Framework\Session\SessionManager;
use Magento\Customer\Model\Session;

class Products extends Template
    const RECENTLY_VIEWED_ACTION_TYPE = 'recently_viewed_product';

    protected CollectionFactory $collectionFactory;

    protected ?Collection $recentlyViewedActionCollection = null;

    protected SessionManager $sessionManager;

    protected Session $customerSession;

    public function __construct(
        Template\Context $context,
        CollectionFactory $collectionFactory,
        Session $customerSession,
        array $data = []
        $this->collectionFactory = $collectionFactory;
        $this->customerSession = $customerSession;
        parent::__construct($context, $data);

    public function getRecentlyCollection()
        $customerId = 0;
        $visitorId = 0;
        if ($this->customerSession->isLoggedIn()) {
            $customerId = $this->customerSession->getCustomerId();
        } else {
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $session = $objectManager->get('\Magento\Framework\Session\SessionManager');
            $sessionData = $session->getData();
            if (array_key_exists('visitor_data', $sessionData)) {
                $visitorId = $sessionData['visitor_data']['visitor_id'];

        if ($customerId || $visitorId) {
            $this->recentlyViewedActionCollection = $this->collectionFactory->create()->addFilterByUserIdentities($customerId, $visitorId)->addFieldToFilter('type_id', self::RECENTLY_VIEWED_ACTION_TYPE)->load();
            $items = $this->recentlyViewedActionCollection->count();

Result : enter image description here

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.