Why php code is getting cached in Magento 2 listing page?












1














I have built in full page cache enabled in Magento.



I have added following code in app/design/frontend/Vendor/Module/Magento_Catalog/templates/product/list.phtml which shows whether an item is already in wishlist or not



<?php $wishlistHelper = $this->helper('MagentoWishlistHelperData'); 
// other code
foreach ($_productCollection as $_product):
$_in_wishlist = false;
foreach ($wishlistHelper->getWishlistItemCollection() as $_wishlist_item){
if(($_product->getId() == $_wishlist_item->getProduct()->getId()) && $customerLoggedin) {
$_in_wishlist = true;
}
}
var_dump($_in_wishlist);
endforeach;
?>


Now I have some code on the same page which adds product to wishlist via Ajax call which works fine.



If I reload the page after product is added to wishlist, above code shows bool(false) but when I clean cache by running bin/magento cache:clean it shows the correct value which is bool(true).



Why this code is getting cached and how to fix it?










share|improve this question






















  • echo anything else, first confirm you are editing correct file ?
    – Wajahat Bashir
    9 hours ago










  • Yes, I am modifying the correct file.
    – amitshree
    8 hours ago
















1














I have built in full page cache enabled in Magento.



I have added following code in app/design/frontend/Vendor/Module/Magento_Catalog/templates/product/list.phtml which shows whether an item is already in wishlist or not



<?php $wishlistHelper = $this->helper('MagentoWishlistHelperData'); 
// other code
foreach ($_productCollection as $_product):
$_in_wishlist = false;
foreach ($wishlistHelper->getWishlistItemCollection() as $_wishlist_item){
if(($_product->getId() == $_wishlist_item->getProduct()->getId()) && $customerLoggedin) {
$_in_wishlist = true;
}
}
var_dump($_in_wishlist);
endforeach;
?>


Now I have some code on the same page which adds product to wishlist via Ajax call which works fine.



If I reload the page after product is added to wishlist, above code shows bool(false) but when I clean cache by running bin/magento cache:clean it shows the correct value which is bool(true).



Why this code is getting cached and how to fix it?










share|improve this question






















  • echo anything else, first confirm you are editing correct file ?
    – Wajahat Bashir
    9 hours ago










  • Yes, I am modifying the correct file.
    – amitshree
    8 hours ago














1












1








1







I have built in full page cache enabled in Magento.



I have added following code in app/design/frontend/Vendor/Module/Magento_Catalog/templates/product/list.phtml which shows whether an item is already in wishlist or not



<?php $wishlistHelper = $this->helper('MagentoWishlistHelperData'); 
// other code
foreach ($_productCollection as $_product):
$_in_wishlist = false;
foreach ($wishlistHelper->getWishlistItemCollection() as $_wishlist_item){
if(($_product->getId() == $_wishlist_item->getProduct()->getId()) && $customerLoggedin) {
$_in_wishlist = true;
}
}
var_dump($_in_wishlist);
endforeach;
?>


Now I have some code on the same page which adds product to wishlist via Ajax call which works fine.



If I reload the page after product is added to wishlist, above code shows bool(false) but when I clean cache by running bin/magento cache:clean it shows the correct value which is bool(true).



Why this code is getting cached and how to fix it?










share|improve this question













I have built in full page cache enabled in Magento.



I have added following code in app/design/frontend/Vendor/Module/Magento_Catalog/templates/product/list.phtml which shows whether an item is already in wishlist or not



<?php $wishlistHelper = $this->helper('MagentoWishlistHelperData'); 
// other code
foreach ($_productCollection as $_product):
$_in_wishlist = false;
foreach ($wishlistHelper->getWishlistItemCollection() as $_wishlist_item){
if(($_product->getId() == $_wishlist_item->getProduct()->getId()) && $customerLoggedin) {
$_in_wishlist = true;
}
}
var_dump($_in_wishlist);
endforeach;
?>


Now I have some code on the same page which adds product to wishlist via Ajax call which works fine.



If I reload the page after product is added to wishlist, above code shows bool(false) but when I clean cache by running bin/magento cache:clean it shows the correct value which is bool(true).



Why this code is getting cached and how to fix it?







magento2 cache






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 10 hours ago









amitshree

2,994103676




2,994103676












  • echo anything else, first confirm you are editing correct file ?
    – Wajahat Bashir
    9 hours ago










  • Yes, I am modifying the correct file.
    – amitshree
    8 hours ago


















  • echo anything else, first confirm you are editing correct file ?
    – Wajahat Bashir
    9 hours ago










  • Yes, I am modifying the correct file.
    – amitshree
    8 hours ago
















echo anything else, first confirm you are editing correct file ?
– Wajahat Bashir
9 hours ago




echo anything else, first confirm you are editing correct file ?
– Wajahat Bashir
9 hours ago












Yes, I am modifying the correct file.
– amitshree
8 hours ago




Yes, I am modifying the correct file.
– amitshree
8 hours ago










1 Answer
1






active

oldest

votes


















2














When FPC is enabled, prefer always factory method. Factory method will create new object. So, it will return always updated data :



By Object Manager :



$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wishlist = $objectManager->create('MagentoWishlistModelWishlist');
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();


By Factory Method :



protected $_wishlistFactory;

public function __construct(
MagentoWishlistModelWishlistFactory $wishlistFactory
){
$this->_wishlistFactory = $wishlistFactory;
}

public function yourFunction()
{
$wishlist = $this->_wishlistFactory->create();
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();
}





share|improve this answer





















  • I tried the object manager way but still it is not reflecting without clearing cache.
    – amitshree
    8 hours ago










  • I just added object manager answer for reference. For better practise use Factory Method. It should be work.
    – Rohan Hapani
    8 hours 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',
autoActivateHeartbeat: false,
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%2f255810%2fwhy-php-code-is-getting-cached-in-magento-2-listing-page%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














When FPC is enabled, prefer always factory method. Factory method will create new object. So, it will return always updated data :



By Object Manager :



$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wishlist = $objectManager->create('MagentoWishlistModelWishlist');
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();


By Factory Method :



protected $_wishlistFactory;

public function __construct(
MagentoWishlistModelWishlistFactory $wishlistFactory
){
$this->_wishlistFactory = $wishlistFactory;
}

public function yourFunction()
{
$wishlist = $this->_wishlistFactory->create();
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();
}





share|improve this answer





















  • I tried the object manager way but still it is not reflecting without clearing cache.
    – amitshree
    8 hours ago










  • I just added object manager answer for reference. For better practise use Factory Method. It should be work.
    – Rohan Hapani
    8 hours ago
















2














When FPC is enabled, prefer always factory method. Factory method will create new object. So, it will return always updated data :



By Object Manager :



$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wishlist = $objectManager->create('MagentoWishlistModelWishlist');
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();


By Factory Method :



protected $_wishlistFactory;

public function __construct(
MagentoWishlistModelWishlistFactory $wishlistFactory
){
$this->_wishlistFactory = $wishlistFactory;
}

public function yourFunction()
{
$wishlist = $this->_wishlistFactory->create();
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();
}





share|improve this answer





















  • I tried the object manager way but still it is not reflecting without clearing cache.
    – amitshree
    8 hours ago










  • I just added object manager answer for reference. For better practise use Factory Method. It should be work.
    – Rohan Hapani
    8 hours ago














2












2








2






When FPC is enabled, prefer always factory method. Factory method will create new object. So, it will return always updated data :



By Object Manager :



$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wishlist = $objectManager->create('MagentoWishlistModelWishlist');
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();


By Factory Method :



protected $_wishlistFactory;

public function __construct(
MagentoWishlistModelWishlistFactory $wishlistFactory
){
$this->_wishlistFactory = $wishlistFactory;
}

public function yourFunction()
{
$wishlist = $this->_wishlistFactory->create();
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();
}





share|improve this answer












When FPC is enabled, prefer always factory method. Factory method will create new object. So, it will return always updated data :



By Object Manager :



$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$wishlist = $objectManager->create('MagentoWishlistModelWishlist');
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();


By Factory Method :



protected $_wishlistFactory;

public function __construct(
MagentoWishlistModelWishlistFactory $wishlistFactory
){
$this->_wishlistFactory = $wishlistFactory;
}

public function yourFunction()
{
$wishlist = $this->_wishlistFactory->create();
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();
}






share|improve this answer












share|improve this answer



share|improve this answer










answered 9 hours ago









Rohan Hapani

5,72121662




5,72121662












  • I tried the object manager way but still it is not reflecting without clearing cache.
    – amitshree
    8 hours ago










  • I just added object manager answer for reference. For better practise use Factory Method. It should be work.
    – Rohan Hapani
    8 hours ago


















  • I tried the object manager way but still it is not reflecting without clearing cache.
    – amitshree
    8 hours ago










  • I just added object manager answer for reference. For better practise use Factory Method. It should be work.
    – Rohan Hapani
    8 hours ago
















I tried the object manager way but still it is not reflecting without clearing cache.
– amitshree
8 hours ago




I tried the object manager way but still it is not reflecting without clearing cache.
– amitshree
8 hours ago












I just added object manager answer for reference. For better practise use Factory Method. It should be work.
– Rohan Hapani
8 hours ago




I just added object manager answer for reference. For better practise use Factory Method. It should be work.
– Rohan Hapani
8 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f255810%2fwhy-php-code-is-getting-cached-in-magento-2-listing-page%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

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo