1

I want to fetch a recently viewed products collection in the observer and by using that collection I want to add my custom product in that collection.

Following Things That I Want To Achieve.

  1. On click of a product when it opens a product detail page that product id should be stored in session->Completed

  2. After that, I want to add those products which are stored in session in recently viewed product collection when the user creates a new account display those products in the recently viewed product widget.->Incomplete

The main Thing Is that I want to display guest user recently viewed product to a new customer when the guestuser creates a new account

Following Is Observer File

<?php 
 namespace Namespace\RecentProducts\Session;
 class ProductData 
  {

protected $_coreSession;
public function __construct(

    \Magento\Framework\Session\SessionManagerInterface $coreSession

){
    $this->_coreSession = $coreSession;
}

public function setValue($value){
$this->_coreSession->start();
$this->_coreSession->setMessage($value);
}

public function getValue(){
$this->_coreSession->start();
return $this->_coreSession->getMessage();
 }

public function unSetValue(){
$this->_coreSession->start();
return $this->_coreSession->unsMessage();
}


}


?>

Following is my session file where I am calling observers methods

<?php

namespace Namespace\RecentProducts\Observer\Product;

use Magento\Framework\Event\Observer;

use Magento\Framework\Event\ObserverInterface;



class Data implements ObserverInterface

{

public function execute(Observer $observer)

{


    $product = $observer->getProduct();

    $originalName = $product->getName();

    $imgdata = $product->getImage();

    $catid = $product->getCategoryId();

    $entid = $product->getEntityId();


    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

/*-------begining of code to set data to session----------*/
$SessionCollection =  $objectManager->get('Magneto\RecentProducts\Session\ProductData');
$value = $SessionCollection->getValue();
$newValue = explode(",",$value);
    if($value)
    {
        if(!in_array($entid,$newValue))
        {
            $newdata = $value.",".$entid;
            $SessionCollection->setValue($newdata);
        }

    }
    else
    {
        $SessionCollection->setValue($entid);

    }

    echo $value;

}

}

Following is events.xml file

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

<event name="catalog_controller_product_view">

    <observer name="RecentProductData" instance="Magneto\RecentProducts\Observer\Product\Data" />

</event>

</config>
0

You can use this code in your observer :

Method 1 :

/**
 * Layout
 * @var \Magento\Framework\View\LayoutInterface
 */

 protected $_layout;

 public function __construct(
    .....
    \Magento\Framework\View\LayoutInterface $layout
    .......
 ) {
     $this->_layout = $layout;
 }

public function getMyCollection() {
    $block =  $this->_layout->getBlockSingleton(\Magento\Reports\Block\Product\Viewed::class)->getItemsCollection();
    return $block;
}

Method 2 :

You need to load ItemCollection() after get collection like below way :

protected $recentlyViewed;

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

public function execute() {
    $collection = $this->recentlyViewed->getItemsCollection()->load();
    echo "<pre>";
    print_r($collection->getData());
    exit;
}
  • Thanks for your answer but not working – Harsh Jayswal Mar 18 at 7:35
  • I have used your second method it returns blank array – Harsh Jayswal Mar 18 at 7:56
  • I tried. It's working perfectly. Please make sure you have collection of recent view product or not and refresh statistics of that grid. – Rohan Hapani Mar 18 at 8:36
  • Yes I have a collection of recent product and also refresh the statistics default recent product is working when i show them using widget but in array it returns blank' – Harsh Jayswal Mar 18 at 8:54
  • Any system or error log? – Rohan Hapani Mar 18 at 12:32

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.