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();
}
magento2 product-collection recently-viewed
add a comment |
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();
}
magento2 product-collection recently-viewed
I added code in chat box.
– Rohan Hapani
yesterday
add a comment |
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();
}
magento2 product-collection recently-viewed
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
magento2 product-collection recently-viewed
edited yesterday
asked 2 days ago
Aditya Shah
3,0911633
3,0911633
I added code in chat box.
– Rohan Hapani
yesterday
add a comment |
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
add a comment |
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.
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
|
show 2 more comments
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;
}
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
|
show 2 more comments
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.
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
|
show 2 more comments
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.
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
|
show 2 more comments
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.
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.
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
|
show 2 more comments
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
|
show 2 more comments
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;
}
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
|
show 2 more comments
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;
}
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
|
show 2 more comments
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;
}
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;
}
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
|
show 2 more comments
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
|
show 2 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I added code in chat box.
– Rohan Hapani
yesterday