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('Magento\Wishlist\Helper\Data');
// 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?