I have tried this but it is not working. I am getting blank array. Suggestions please, i am stuck.

I want to display recently viewed product in owl carousel. But getting no products.

protected $recentlyViewed;

public function __construct(\Magento\Reports\Block\Product\Viewed $recentlyViewed ) {
    $this->recentlyViewed = $recentlyViewed;

public function execute() {
    $collection = $this->recentlyViewed->getItemsCollection()->load();
    echo "<pre>";

I also tried this, but still blank array.

protected $_viewProductsBlock;

public function __construct(\Magento\Reports\Block\Product\Widget\Viewed\Proxy $viewedProductsBlock) {
    $this->_viewProductsBlock         = $viewedProductsBlock;
public function execute() {
    $_collection = $this->_viewProductsBlock->getItemsCollection();
    echo "<pre>";
    echo "</pre>";
  • Have you check that do you get data into the collection? Apr 23, 2019 at 11:03
  • Yes, there are so many in recently viewed, but still i am getting blank array. Apr 23, 2019 at 11:05
  • if my answer help you, please mark as solution as well, so it help other into community as well. Apr 23, 2019 at 11:29
  • Any solution here?
    – Ramya
    Sep 3, 2020 at 15:36

2 Answers 2


You need to use this code :

namespace Vendor\Extension\Block\RecentlyViewed;

class Test extends \Magento\Framework\View\Element\Template
    protected $recentlyViewed;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Reports\Block\Product\Viewed $recentlyViewed,
        array $data = []
    ) {
        $this->recentlyViewed = $recentlyViewed;
        parent::__construct( $context, $data );

    public function getMostRecentlyViewed(){
        return $this->recentlyViewed->getItemsCollection()->getData();

Then you can call this block according to your requirement.

  • I also tried this, but not working. Apr 23, 2019 at 12:14
  • Kindly confrim this things. 1. You replace the path into above code. 2. Run the magento commands after above changes and then check. Apr 23, 2019 at 12:20

add cacheable="false" in CMS page block as Mage is tracking visitor id for cached CMS pages which is returning blank.

{{block class="CP\Recentproducts\Block\RecentlyViewed" name="recentproduct_recentlyviewed" template="CP_Recentproducts::recentlyviewed.phtml" cacheable="false" }}

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.