Why php code is getting cached in Magento 2 listing page?
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
add a comment |
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
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
add a comment |
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
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
magento2 cache
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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();
}
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 addedecho "test";
on the top oflist.phtml
. I changed it toecho "test 2";
. Which is not reflecting if I do not clear cache.
– amitshree
yesterday
add a comment |
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
});
}
});
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%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
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();
}
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 addedecho "test";
on the top oflist.phtml
. I changed it toecho "test 2";
. Which is not reflecting if I do not clear cache.
– amitshree
yesterday
add a comment |
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();
}
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 addedecho "test";
on the top oflist.phtml
. I changed it toecho "test 2";
. Which is not reflecting if I do not clear cache.
– amitshree
yesterday
add a comment |
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();
}
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();
}
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 addedecho "test";
on the top oflist.phtml
. I changed it toecho "test 2";
. Which is not reflecting if I do not clear cache.
– amitshree
yesterday
add a comment |
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 addedecho "test";
on the top oflist.phtml
. I changed it toecho "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
add a comment |
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.
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%2f255810%2fwhy-php-code-is-getting-cached-in-magento-2-listing-page%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
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