How to get recently viewed products collection of Customer Magento 2?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
1
down vote

favorite













Magento 2 get recently viewed products collection of Customer (Using customer ID) programmatically




I tried this solution But I want recently viewed products collection.



I tried below solution too but it is not returning anything. [I am logged in]



protected $recentlyViewed;

public function __construct(
...
MagentoReportsBlockProductViewed $recentlyViewed
) {
...
$this->recentlyViewed = $recentlyViewed;
}

/**
* Get recently viewed products for the customer
*
*/
public function getMostRecentlyViewed(){
return $this->recentlyViewed->getItemsCollection();
}









share|improve this question






























    up vote
    1
    down vote

    favorite













    Magento 2 get recently viewed products collection of Customer (Using customer ID) programmatically




    I tried this solution But I want recently viewed products collection.



    I tried below solution too but it is not returning anything. [I am logged in]



    protected $recentlyViewed;

    public function __construct(
    ...
    MagentoReportsBlockProductViewed $recentlyViewed
    ) {
    ...
    $this->recentlyViewed = $recentlyViewed;
    }

    /**
    * Get recently viewed products for the customer
    *
    */
    public function getMostRecentlyViewed(){
    return $this->recentlyViewed->getItemsCollection();
    }









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite












      Magento 2 get recently viewed products collection of Customer (Using customer ID) programmatically




      I tried this solution But I want recently viewed products collection.



      I tried below solution too but it is not returning anything. [I am logged in]



      protected $recentlyViewed;

      public function __construct(
      ...
      MagentoReportsBlockProductViewed $recentlyViewed
      ) {
      ...
      $this->recentlyViewed = $recentlyViewed;
      }

      /**
      * Get recently viewed products for the customer
      *
      */
      public function getMostRecentlyViewed(){
      return $this->recentlyViewed->getItemsCollection();
      }









      share|improve this question
















      Magento 2 get recently viewed products collection of Customer (Using customer ID) programmatically




      I tried this solution But I want recently viewed products collection.



      I tried below solution too but it is not returning anything. [I am logged in]



      protected $recentlyViewed;

      public function __construct(
      ...
      MagentoReportsBlockProductViewed $recentlyViewed
      ) {
      ...
      $this->recentlyViewed = $recentlyViewed;
      }

      /**
      * Get recently viewed products for the customer
      *
      */
      public function getMostRecentlyViewed(){
      return $this->recentlyViewed->getItemsCollection();
      }






      magento2 product-collection recently-viewed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago

























      asked 20 hours ago









      Aditya Shah

      3,0911633




      3,0911633






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          Please try below code, I tested the same and found it working:



          <?php 
          namespace YourPackageYourModuleBlockRecentlyViewed;

          class Test extends MagentoFrameworkViewElementTemplate
          {
          protected $recentlyViewed;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoReportsBlockProductViewed $recentlyViewed,
          array $data =
          ) {
          $this->recentlyViewed = $recentlyViewed;
          parent::__construct( $context, $data );
          }

          public function getMostRecentlyViewed(){
          return $this->recentlyViewed->getItemsCollection()->getData();
          }
          }


          and call this block in required file, cms page or static block, for example if you wan to add this block to home page than add below code:




          {{block class="YourPackageYourModuleBlockRecentlyViewedTest"
          name="block_recently_viewed"
          template="YourPackage_YourModule::test.phtml"}}




          In test.phtml file you can get the collection and design it in your way, I just tested by printing the collection and it was printing correctly.



          test.phtml



          <?php echo "<pre>"; print_r($this->getMostRecentlyViewed()); ?>


          Note : Please note that the collection will be empty if you don't visits any page as the collection is of recently viewed product by you. Visit some products at your store and you will get the collection being populated.






          share|improve this answer





















          • Is it possible without accessing parent ?
            – Aditya Shah
            1 hour ago










          • without extends MagentoFrameworkViewElementTemplate
            – Aditya Shah
            1 hour ago










          • No, it would create issue as we need to Abstract class for frontend representational purpose :), We need to create similar abstract class if you don't want to extend it but that is meaningless effort. Its good to extend the existing available Abstract class..
            – Himmat Paliwal
            1 hour ago












          • Okay and If i extend that class _ I want customers collection
            – Aditya Shah
            1 hour ago










          • And I want all this in Model file that's why I asked that I need collection for that :)
            – Aditya Shah
            1 hour ago


















          up vote
          2
          down vote













          Try to use this code :



          Method 1 :



          /**
          * Layout
          * @var MagentoFrameworkViewLayoutInterface
          */

          protected $_layout;

          public function __construct(
          .....
          MagentoFrameworkViewLayoutInterface $layout
          .......
          ) {
          $this->_layout = $layout;
          }

          public function getMyCollection() {
          $block = $this->_layout->getBlockSingleton(MagentoReportsBlockProductViewed::class)->getItemsCollection();
          return $block;
          }


          UPDATE :



          Method 2 :



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



          protected $recentlyViewed;

          public function __construct(
          MagentoReportsBlockProductViewed $recentlyViewed
          ) {
          $this->recentlyViewed = $recentlyViewed;
          }

          public function execute() {
          $collection = $this->recentlyViewed->getItemsCollection()->load();
          echo "<pre>";
          print_r($collection->getData());
          exit;
          }





          share|improve this answer



















          • 1




            Thanks bro :) Definitely try this tomorrow morning !
            – Aditya Shah
            17 hours ago






          • 1




            Sure :) If not working then you can update here..
            – Rohan Hapani
            17 hours ago










          • Not working @Rohan
            – Aditya Shah
            1 hour ago






          • 1




            Try to use this second method.. it's working from my side so.
            – Rohan Hapani
            1 hour ago










          • Okay bro :) trying *
            – Aditya Shah
            1 hour ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f251219%2fhow-to-get-recently-viewed-products-collection-of-customer-magento-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          Please try below code, I tested the same and found it working:



          <?php 
          namespace YourPackageYourModuleBlockRecentlyViewed;

          class Test extends MagentoFrameworkViewElementTemplate
          {
          protected $recentlyViewed;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoReportsBlockProductViewed $recentlyViewed,
          array $data =
          ) {
          $this->recentlyViewed = $recentlyViewed;
          parent::__construct( $context, $data );
          }

          public function getMostRecentlyViewed(){
          return $this->recentlyViewed->getItemsCollection()->getData();
          }
          }


          and call this block in required file, cms page or static block, for example if you wan to add this block to home page than add below code:




          {{block class="YourPackageYourModuleBlockRecentlyViewedTest"
          name="block_recently_viewed"
          template="YourPackage_YourModule::test.phtml"}}




          In test.phtml file you can get the collection and design it in your way, I just tested by printing the collection and it was printing correctly.



          test.phtml



          <?php echo "<pre>"; print_r($this->getMostRecentlyViewed()); ?>


          Note : Please note that the collection will be empty if you don't visits any page as the collection is of recently viewed product by you. Visit some products at your store and you will get the collection being populated.






          share|improve this answer





















          • Is it possible without accessing parent ?
            – Aditya Shah
            1 hour ago










          • without extends MagentoFrameworkViewElementTemplate
            – Aditya Shah
            1 hour ago










          • No, it would create issue as we need to Abstract class for frontend representational purpose :), We need to create similar abstract class if you don't want to extend it but that is meaningless effort. Its good to extend the existing available Abstract class..
            – Himmat Paliwal
            1 hour ago












          • Okay and If i extend that class _ I want customers collection
            – Aditya Shah
            1 hour ago










          • And I want all this in Model file that's why I asked that I need collection for that :)
            – Aditya Shah
            1 hour ago















          up vote
          2
          down vote













          Please try below code, I tested the same and found it working:



          <?php 
          namespace YourPackageYourModuleBlockRecentlyViewed;

          class Test extends MagentoFrameworkViewElementTemplate
          {
          protected $recentlyViewed;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoReportsBlockProductViewed $recentlyViewed,
          array $data =
          ) {
          $this->recentlyViewed = $recentlyViewed;
          parent::__construct( $context, $data );
          }

          public function getMostRecentlyViewed(){
          return $this->recentlyViewed->getItemsCollection()->getData();
          }
          }


          and call this block in required file, cms page or static block, for example if you wan to add this block to home page than add below code:




          {{block class="YourPackageYourModuleBlockRecentlyViewedTest"
          name="block_recently_viewed"
          template="YourPackage_YourModule::test.phtml"}}




          In test.phtml file you can get the collection and design it in your way, I just tested by printing the collection and it was printing correctly.



          test.phtml



          <?php echo "<pre>"; print_r($this->getMostRecentlyViewed()); ?>


          Note : Please note that the collection will be empty if you don't visits any page as the collection is of recently viewed product by you. Visit some products at your store and you will get the collection being populated.






          share|improve this answer





















          • Is it possible without accessing parent ?
            – Aditya Shah
            1 hour ago










          • without extends MagentoFrameworkViewElementTemplate
            – Aditya Shah
            1 hour ago










          • No, it would create issue as we need to Abstract class for frontend representational purpose :), We need to create similar abstract class if you don't want to extend it but that is meaningless effort. Its good to extend the existing available Abstract class..
            – Himmat Paliwal
            1 hour ago












          • Okay and If i extend that class _ I want customers collection
            – Aditya Shah
            1 hour ago










          • And I want all this in Model file that's why I asked that I need collection for that :)
            – Aditya Shah
            1 hour ago













          up vote
          2
          down vote










          up vote
          2
          down vote









          Please try below code, I tested the same and found it working:



          <?php 
          namespace YourPackageYourModuleBlockRecentlyViewed;

          class Test extends MagentoFrameworkViewElementTemplate
          {
          protected $recentlyViewed;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoReportsBlockProductViewed $recentlyViewed,
          array $data =
          ) {
          $this->recentlyViewed = $recentlyViewed;
          parent::__construct( $context, $data );
          }

          public function getMostRecentlyViewed(){
          return $this->recentlyViewed->getItemsCollection()->getData();
          }
          }


          and call this block in required file, cms page or static block, for example if you wan to add this block to home page than add below code:




          {{block class="YourPackageYourModuleBlockRecentlyViewedTest"
          name="block_recently_viewed"
          template="YourPackage_YourModule::test.phtml"}}




          In test.phtml file you can get the collection and design it in your way, I just tested by printing the collection and it was printing correctly.



          test.phtml



          <?php echo "<pre>"; print_r($this->getMostRecentlyViewed()); ?>


          Note : Please note that the collection will be empty if you don't visits any page as the collection is of recently viewed product by you. Visit some products at your store and you will get the collection being populated.






          share|improve this answer












          Please try below code, I tested the same and found it working:



          <?php 
          namespace YourPackageYourModuleBlockRecentlyViewed;

          class Test extends MagentoFrameworkViewElementTemplate
          {
          protected $recentlyViewed;

          public function __construct(
          MagentoFrameworkViewElementTemplateContext $context,
          MagentoReportsBlockProductViewed $recentlyViewed,
          array $data =
          ) {
          $this->recentlyViewed = $recentlyViewed;
          parent::__construct( $context, $data );
          }

          public function getMostRecentlyViewed(){
          return $this->recentlyViewed->getItemsCollection()->getData();
          }
          }


          and call this block in required file, cms page or static block, for example if you wan to add this block to home page than add below code:




          {{block class="YourPackageYourModuleBlockRecentlyViewedTest"
          name="block_recently_viewed"
          template="YourPackage_YourModule::test.phtml"}}




          In test.phtml file you can get the collection and design it in your way, I just tested by printing the collection and it was printing correctly.



          test.phtml



          <?php echo "<pre>"; print_r($this->getMostRecentlyViewed()); ?>


          Note : Please note that the collection will be empty if you don't visits any page as the collection is of recently viewed product by you. Visit some products at your store and you will get the collection being populated.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 15 hours ago









          Himmat Paliwal

          718417




          718417












          • Is it possible without accessing parent ?
            – Aditya Shah
            1 hour ago










          • without extends MagentoFrameworkViewElementTemplate
            – Aditya Shah
            1 hour ago










          • No, it would create issue as we need to Abstract class for frontend representational purpose :), We need to create similar abstract class if you don't want to extend it but that is meaningless effort. Its good to extend the existing available Abstract class..
            – Himmat Paliwal
            1 hour ago












          • Okay and If i extend that class _ I want customers collection
            – Aditya Shah
            1 hour ago










          • And I want all this in Model file that's why I asked that I need collection for that :)
            – Aditya Shah
            1 hour ago


















          • Is it possible without accessing parent ?
            – Aditya Shah
            1 hour ago










          • without extends MagentoFrameworkViewElementTemplate
            – Aditya Shah
            1 hour ago










          • No, it would create issue as we need to Abstract class for frontend representational purpose :), We need to create similar abstract class if you don't want to extend it but that is meaningless effort. Its good to extend the existing available Abstract class..
            – Himmat Paliwal
            1 hour ago












          • Okay and If i extend that class _ I want customers collection
            – Aditya Shah
            1 hour ago










          • And I want all this in Model file that's why I asked that I need collection for that :)
            – Aditya Shah
            1 hour ago
















          Is it possible without accessing parent ?
          – Aditya Shah
          1 hour ago




          Is it possible without accessing parent ?
          – Aditya Shah
          1 hour ago












          without extends MagentoFrameworkViewElementTemplate
          – Aditya Shah
          1 hour ago




          without extends MagentoFrameworkViewElementTemplate
          – Aditya Shah
          1 hour ago












          No, it would create issue as we need to Abstract class for frontend representational purpose :), We need to create similar abstract class if you don't want to extend it but that is meaningless effort. Its good to extend the existing available Abstract class..
          – Himmat Paliwal
          1 hour ago






          No, it would create issue as we need to Abstract class for frontend representational purpose :), We need to create similar abstract class if you don't want to extend it but that is meaningless effort. Its good to extend the existing available Abstract class..
          – Himmat Paliwal
          1 hour ago














          Okay and If i extend that class _ I want customers collection
          – Aditya Shah
          1 hour ago




          Okay and If i extend that class _ I want customers collection
          – Aditya Shah
          1 hour ago












          And I want all this in Model file that's why I asked that I need collection for that :)
          – Aditya Shah
          1 hour ago




          And I want all this in Model file that's why I asked that I need collection for that :)
          – Aditya Shah
          1 hour ago












          up vote
          2
          down vote













          Try to use this code :



          Method 1 :



          /**
          * Layout
          * @var MagentoFrameworkViewLayoutInterface
          */

          protected $_layout;

          public function __construct(
          .....
          MagentoFrameworkViewLayoutInterface $layout
          .......
          ) {
          $this->_layout = $layout;
          }

          public function getMyCollection() {
          $block = $this->_layout->getBlockSingleton(MagentoReportsBlockProductViewed::class)->getItemsCollection();
          return $block;
          }


          UPDATE :



          Method 2 :



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



          protected $recentlyViewed;

          public function __construct(
          MagentoReportsBlockProductViewed $recentlyViewed
          ) {
          $this->recentlyViewed = $recentlyViewed;
          }

          public function execute() {
          $collection = $this->recentlyViewed->getItemsCollection()->load();
          echo "<pre>";
          print_r($collection->getData());
          exit;
          }





          share|improve this answer



















          • 1




            Thanks bro :) Definitely try this tomorrow morning !
            – Aditya Shah
            17 hours ago






          • 1




            Sure :) If not working then you can update here..
            – Rohan Hapani
            17 hours ago










          • Not working @Rohan
            – Aditya Shah
            1 hour ago






          • 1




            Try to use this second method.. it's working from my side so.
            – Rohan Hapani
            1 hour ago










          • Okay bro :) trying *
            – Aditya Shah
            1 hour ago















          up vote
          2
          down vote













          Try to use this code :



          Method 1 :



          /**
          * Layout
          * @var MagentoFrameworkViewLayoutInterface
          */

          protected $_layout;

          public function __construct(
          .....
          MagentoFrameworkViewLayoutInterface $layout
          .......
          ) {
          $this->_layout = $layout;
          }

          public function getMyCollection() {
          $block = $this->_layout->getBlockSingleton(MagentoReportsBlockProductViewed::class)->getItemsCollection();
          return $block;
          }


          UPDATE :



          Method 2 :



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



          protected $recentlyViewed;

          public function __construct(
          MagentoReportsBlockProductViewed $recentlyViewed
          ) {
          $this->recentlyViewed = $recentlyViewed;
          }

          public function execute() {
          $collection = $this->recentlyViewed->getItemsCollection()->load();
          echo "<pre>";
          print_r($collection->getData());
          exit;
          }





          share|improve this answer



















          • 1




            Thanks bro :) Definitely try this tomorrow morning !
            – Aditya Shah
            17 hours ago






          • 1




            Sure :) If not working then you can update here..
            – Rohan Hapani
            17 hours ago










          • Not working @Rohan
            – Aditya Shah
            1 hour ago






          • 1




            Try to use this second method.. it's working from my side so.
            – Rohan Hapani
            1 hour ago










          • Okay bro :) trying *
            – Aditya Shah
            1 hour ago













          up vote
          2
          down vote










          up vote
          2
          down vote









          Try to use this code :



          Method 1 :



          /**
          * Layout
          * @var MagentoFrameworkViewLayoutInterface
          */

          protected $_layout;

          public function __construct(
          .....
          MagentoFrameworkViewLayoutInterface $layout
          .......
          ) {
          $this->_layout = $layout;
          }

          public function getMyCollection() {
          $block = $this->_layout->getBlockSingleton(MagentoReportsBlockProductViewed::class)->getItemsCollection();
          return $block;
          }


          UPDATE :



          Method 2 :



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



          protected $recentlyViewed;

          public function __construct(
          MagentoReportsBlockProductViewed $recentlyViewed
          ) {
          $this->recentlyViewed = $recentlyViewed;
          }

          public function execute() {
          $collection = $this->recentlyViewed->getItemsCollection()->load();
          echo "<pre>";
          print_r($collection->getData());
          exit;
          }





          share|improve this answer














          Try to use this code :



          Method 1 :



          /**
          * Layout
          * @var MagentoFrameworkViewLayoutInterface
          */

          protected $_layout;

          public function __construct(
          .....
          MagentoFrameworkViewLayoutInterface $layout
          .......
          ) {
          $this->_layout = $layout;
          }

          public function getMyCollection() {
          $block = $this->_layout->getBlockSingleton(MagentoReportsBlockProductViewed::class)->getItemsCollection();
          return $block;
          }


          UPDATE :



          Method 2 :



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



          protected $recentlyViewed;

          public function __construct(
          MagentoReportsBlockProductViewed $recentlyViewed
          ) {
          $this->recentlyViewed = $recentlyViewed;
          }

          public function execute() {
          $collection = $this->recentlyViewed->getItemsCollection()->load();
          echo "<pre>";
          print_r($collection->getData());
          exit;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 17 hours ago









          Rohan Hapani

          4,98521559




          4,98521559








          • 1




            Thanks bro :) Definitely try this tomorrow morning !
            – Aditya Shah
            17 hours ago






          • 1




            Sure :) If not working then you can update here..
            – Rohan Hapani
            17 hours ago










          • Not working @Rohan
            – Aditya Shah
            1 hour ago






          • 1




            Try to use this second method.. it's working from my side so.
            – Rohan Hapani
            1 hour ago










          • Okay bro :) trying *
            – Aditya Shah
            1 hour ago














          • 1




            Thanks bro :) Definitely try this tomorrow morning !
            – Aditya Shah
            17 hours ago






          • 1




            Sure :) If not working then you can update here..
            – Rohan Hapani
            17 hours ago










          • Not working @Rohan
            – Aditya Shah
            1 hour ago






          • 1




            Try to use this second method.. it's working from my side so.
            – Rohan Hapani
            1 hour ago










          • Okay bro :) trying *
            – Aditya Shah
            1 hour ago








          1




          1




          Thanks bro :) Definitely try this tomorrow morning !
          – Aditya Shah
          17 hours ago




          Thanks bro :) Definitely try this tomorrow morning !
          – Aditya Shah
          17 hours ago




          1




          1




          Sure :) If not working then you can update here..
          – Rohan Hapani
          17 hours ago




          Sure :) If not working then you can update here..
          – Rohan Hapani
          17 hours ago












          Not working @Rohan
          – Aditya Shah
          1 hour ago




          Not working @Rohan
          – Aditya Shah
          1 hour ago




          1




          1




          Try to use this second method.. it's working from my side so.
          – Rohan Hapani
          1 hour ago




          Try to use this second method.. it's working from my side so.
          – Rohan Hapani
          1 hour ago












          Okay bro :) trying *
          – Aditya Shah
          1 hour ago




          Okay bro :) trying *
          – Aditya Shah
          1 hour ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f251219%2fhow-to-get-recently-viewed-products-collection-of-customer-magento-2%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          數位音樂下載

          When can things happen in Etherscan, such as the picture below?

          格利澤436b