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
























  • I added code in chat box.
    – Rohan Hapani
    yesterday

















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
























  • I added code in chat box.
    – Rohan Hapani
    yesterday













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 yesterday

























asked 2 days ago









Aditya Shah

3,0911633




3,0911633












  • I added code in chat box.
    – Rohan Hapani
    yesterday


















  • I added code in chat box.
    – Rohan Hapani
    yesterday
















I added code in chat box.
– Rohan Hapani
yesterday




I added code in chat box.
– Rohan Hapani
yesterday










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
    yesterday










  • without extends MagentoFrameworkViewElementTemplate
    – Aditya Shah
    yesterday










  • 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
    yesterday












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










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


















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
    2 days ago






  • 1




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










  • Not working @Rohan
    – Aditya Shah
    yesterday






  • 1




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










  • Okay bro :) trying *
    – Aditya Shah
    yesterday











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
    yesterday










  • without extends MagentoFrameworkViewElementTemplate
    – Aditya Shah
    yesterday










  • 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
    yesterday












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










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















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
    yesterday










  • without extends MagentoFrameworkViewElementTemplate
    – Aditya Shah
    yesterday










  • 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
    yesterday












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










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













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 2 days ago









Himmat Paliwal

768417




768417












  • Is it possible without accessing parent ?
    – Aditya Shah
    yesterday










  • without extends MagentoFrameworkViewElementTemplate
    – Aditya Shah
    yesterday










  • 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
    yesterday












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










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


















  • Is it possible without accessing parent ?
    – Aditya Shah
    yesterday










  • without extends MagentoFrameworkViewElementTemplate
    – Aditya Shah
    yesterday










  • 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
    yesterday












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










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
















Is it possible without accessing parent ?
– Aditya Shah
yesterday




Is it possible without accessing parent ?
– Aditya Shah
yesterday












without extends MagentoFrameworkViewElementTemplate
– Aditya Shah
yesterday




without extends MagentoFrameworkViewElementTemplate
– Aditya Shah
yesterday












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
yesterday






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
yesterday














Okay and If i extend that class _ I want customers collection
– Aditya Shah
yesterday




Okay and If i extend that class _ I want customers collection
– Aditya Shah
yesterday












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




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












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
    2 days ago






  • 1




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










  • Not working @Rohan
    – Aditya Shah
    yesterday






  • 1




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










  • Okay bro :) trying *
    – Aditya Shah
    yesterday















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
    2 days ago






  • 1




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










  • Not working @Rohan
    – Aditya Shah
    yesterday






  • 1




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










  • Okay bro :) trying *
    – Aditya Shah
    yesterday













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 yesterday

























answered 2 days ago









Rohan Hapani

5,10021559




5,10021559








  • 1




    Thanks bro :) Definitely try this tomorrow morning !
    – Aditya Shah
    2 days ago






  • 1




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










  • Not working @Rohan
    – Aditya Shah
    yesterday






  • 1




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










  • Okay bro :) trying *
    – Aditya Shah
    yesterday














  • 1




    Thanks bro :) Definitely try this tomorrow morning !
    – Aditya Shah
    2 days ago






  • 1




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










  • Not working @Rohan
    – Aditya Shah
    yesterday






  • 1




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










  • Okay bro :) trying *
    – Aditya Shah
    yesterday








1




1




Thanks bro :) Definitely try this tomorrow morning !
– Aditya Shah
2 days ago




Thanks bro :) Definitely try this tomorrow morning !
– Aditya Shah
2 days ago




1




1




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




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












Not working @Rohan
– Aditya Shah
yesterday




Not working @Rohan
– Aditya Shah
yesterday




1




1




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




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












Okay bro :) trying *
– Aditya Shah
yesterday




Okay bro :) trying *
– Aditya Shah
yesterday


















 

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

Morgemoulin

Scott Moir

Souastre