1

I want to get the Recently Viewed Products without using Widget or static Block I tried this

namespace PME\Hideprice\Block;

class getTestingData 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 testing(){
        return $this->recentlyViewed->getItemsCollection()->getData();
        //return "Asad Khan";
    }
}

I also tried

<?php echo $this->getLayout()->createBlock("Magento\Reports\Block\Product\Widget\Viewed")->setDisplayType("recently.view.products")->setProductsCount("5")->setTemplate("widget/viewed/content/viewed_list.phtml")->toHtml(); ?>

but both of these does not return me anything

but these classes does not return me anything. How to get all the recently viewed product?

7
  • please try injecting this block \Magento\Catalog\Block\Widget\RecentlyViewed instead Commented Jun 19, 2019 at 8:35
  • Sir I tried that one also didn't return me anything
    – Asad Khan
    Commented Jun 19, 2019 at 8:36
  • do you see recently viewed products in your widget? Commented Jun 19, 2019 at 8:43
  • Yeah but I don't want to get the recently viewed data by widget. I want to get it by functions
    – Asad Khan
    Commented Jun 19, 2019 at 8:46
  • please verify that there are actually recently viewed products! maybe that's the issue. you can't assume that you click on a product and it will show up in your collection. Commented Jun 19, 2019 at 8:49

1 Answer 1

0

Try This :-

namespace PME\Hideprice\Block;

class getTestingData extends \Magento\Framework\View\Element\Template
{
    protected $_recentlyViewed;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Reports\Block\Product\Viewed $recentlyViewed,
        array $data = []
    ){
        $this->_recentlyViewed = $recentlyViewed;
        parent::__construct($context, $data);
    }  
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    public function testing()
    {
        $collection = $this->_recentlyViewed->getItemsCollection();
        foreach($collection as $data)
        {
             $info = $data->getData();
             print_r($info);      
        }
    }
}
3
  • Sorry Sir but this isn't working returning null array like array()
    – Asad Khan
    Commented Jun 19, 2019 at 13:45
  • $data->getData() returning null array ?? Commented Jun 19, 2019 at 13:48
  • Yes Sir it gives me null array also when I tried return $this->recentlyViewed->getItemsCollection()->getData(); or return $this->recentlyViewed->getItemsCollection()->load()->getData(); n these r also sending me null array
    – Asad Khan
    Commented Jun 19, 2019 at 13:57

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.