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
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 1 hour ago
asked 20 hours ago
Aditya Shah
3,0911633
3,0911633
add a comment |
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
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
|
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
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
|
show 1 more comment
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
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
|
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
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
|
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 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
|
show 2 more comments
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
|
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
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
|
show 1 more comment
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
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
|
show 1 more comment
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 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
|
show 1 more comment
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
|
show 1 more comment
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