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? – Dhiren Vasoya Apr 23 '19 at 11:03
  • Yes, there are so many in recently viewed, but still i am getting blank array. – Shweta Danej Apr 23 '19 at 11:05
  • if my answer help you, please mark as solution as well, so it help other into community as well. – Dhiren Vasoya Apr 23 '19 at 11:29
  • Any solution here? – Ramya Sep 3 at 15:36

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.

| improve this answer | |
  • I also tried this, but not working. – Shweta Danej Apr 23 '19 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. – Dhiren Vasoya Apr 23 '19 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" }}
| improve this answer | |

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.