1

I have created a custom graphQL to get the recently viewed products.

I used the following code .

<?php
class RecentlyViewed implements Magento\Framework\GraphQl\Query\ResolverInterface
{
 protected $appEmulation;
 protected $recentProducts;
 public function __construct(       
    \Magento\Reports\Block\Product\Viewed $recentProducts,
    \Magento\Store\Model\App\Emulation $appEmulation,
) {    
    $this->appEmulation = $appEmulation;    
    $this->recentProducts= $recentProducts;     

}

public function resolve(
    \Magento\Framework\GraphQl\Schema\Type\ResolveInfo $info,
    array $value=null,
    array $args=null
) {
   
    $this->appEmulation->startEnvironmentEmulation(1, \Magento\Framework\App\Area::AREA_FRONTEND, true);
    $collection = $this->recentProducts->getItemsCollection()->load();      
    $this->appEmulation ->stopEnvironmentEmulation();
    return $collection->getData();
  }
}

This is returning empty collection. Can anyone suggest me how to get the recently viewed collection in graphQL. Thanks in Advance!.

4
  • Recent View collection only loads for logged in customer not for not logged in customers
    – Vivek
    Feb 6 at 12:42
  • @Vivek, I don't think so, bcz even for guest users also this shows
    – Manjunath
    Feb 7 at 6:22
  • Collection will be empty for guest users
    – Vivek
    Feb 7 at 7:44
  • How to get this for guest also?
    – Manjunath
    Feb 10 at 6:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.