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










  • Yes, I am modifying the correct file.
    – amitshree
    2 days 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
    2 days ago










  • Yes, I am modifying the correct file.
    – amitshree
    2 days 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 2 days ago









amitshree

3,004103776




3,004103776












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










  • Yes, I am modifying the correct file.
    – amitshree
    2 days ago


















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










  • Yes, I am modifying the correct file.
    – amitshree
    2 days ago
















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




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












Yes, I am modifying the correct file.
– amitshree
2 days ago




Yes, I am modifying the correct file.
– amitshree
2 days 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
    2 days ago










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










  • I have added echo "test"; on the top of list.phtml. I changed it to echo "test 2";. Which is not reflecting if I do not clear cache.
    – amitshree
    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',
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
    2 days ago










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










  • I have added echo "test"; on the top of list.phtml. I changed it to echo "test 2";. Which is not reflecting if I do not clear cache.
    – amitshree
    yesterday
















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










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










  • I have added echo "test"; on the top of list.phtml. I changed it to echo "test 2";. Which is not reflecting if I do not clear cache.
    – amitshree
    yesterday














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









Rohan Hapani

5,71921662




5,71921662












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










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










  • I have added echo "test"; on the top of list.phtml. I changed it to echo "test 2";. Which is not reflecting if I do not clear cache.
    – amitshree
    yesterday


















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










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










  • I have added echo "test"; on the top of list.phtml. I changed it to echo "test 2";. Which is not reflecting if I do not clear cache.
    – amitshree
    yesterday
















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




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












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




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












I have added echo "test"; on the top of list.phtml. I changed it to echo "test 2";. Which is not reflecting if I do not clear cache.
– amitshree
yesterday




I have added echo "test"; on the top of list.phtml. I changed it to echo "test 2";. Which is not reflecting if I do not clear cache.
– amitshree
yesterday


















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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?