0

i want to get all data from magento core table table name is

report_viewed_product_index

i searched on stakeexchange and found some solution but havn't for success

class Wbtab 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 getOrderItems()
    {
        echo "<pre>";
        $collection = $this->_recentlyViewed->getItemsCollection();

        print_r($collection->load()->getData());
        exit();

    }
}
4
  • get recent view product collection ???? Commented Jun 12, 2019 at 10:04
  • yesss actually that what i want but i am gettingnull when tried that code
    – Waqar Ali
    Commented Jun 12, 2019 at 10:06
  • try this print_r($collection->getData()); Commented Jun 12, 2019 at 10:08
  • tried that already still getting same
    – Waqar Ali
    Commented Jun 12, 2019 at 10:10

2 Answers 2

1

Try This :-

class Wbtab 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 getOrderItems()
    {
        echo "<pre>";
        $collection = $this->_recentlyViewed->getItemsCollection();
        foreach($collection as $data)
        {
             $info = $data->getData();
             print_r($info);      
        }
    }
}
6
  • thank you.. its working
    – Waqar Ali
    Commented Jun 12, 2019 at 10:23
  • wel come..happy coding...:) Commented Jun 12, 2019 at 10:27
  • can i add filters to collection like product_id filter or date filter
    – Waqar Ali
    Commented Jun 12, 2019 at 10:49
  • let me check.... Commented Jun 12, 2019 at 10:51
  • another thing when i logout user i returns null
    – Waqar Ali
    Commented Jun 12, 2019 at 10:58
0

try this ...... definatly work

public function __construct(
    \Magento\Reports\Block\Product\Viewed $recentlyViewed,
    array $data = []
){
    $this->recentlyViewed = $recentlyViewed;
} 
public function getMostRecentlyViewed()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$recentlyViewedClass = \Magento\Reports\Model\Product\Index\Viewed::class;
$recentlyViewedProductCollection = $objectManager->create($recentlyViewedClass)->getCollection();
$recentlyViewedProductCollection->addAttributeToSelect('*')
                                ->setPageSize(5) // load 10 products
                                ->load();

if($recentlyViewedProductCollection->getSize() > 0 )   {
 
    $product['message'] = "";
    foreach($recentlyViewedProductCollection as $item) {
        $data[]= $item->getData();
        $product['data'] = $data;
    }
    echo json_encode($product);
    die;
}

} }

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.