How to remove cache for Modal?

by Ramesh KR   Last Updated January 12, 2018 09:09 AM

I created modal in homepage when user logged in. This is my Phtml. Everything working fine but data updates only when I flush cache. How to clean cache on load page.

<?php 

$money = $block->walletMoney();

$om = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $om->create('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
    $name =  $customerSession->getCustomer()->getName(); 
    $idd =  $customerSession->getCustomer()->getId();

    if($money>0){

     echo "<div id='custom-popup-modal' style='display:block'>
    <h1> Congragulations, <strong>$name</strong> </h1>
    <h2>Your Wallet Money of Rs.$money is Credited</h2>
    <a href='https://test.rkhomeappliances.co.in/otp/index/addmoney'>Collect Credits</a>
    </div>";

}

}
?>
<script>
require(
    [
        'jquery',
        'Magento_Ui/js/modal/modal'
    ],
    function(
        $,
        modal
    ) {
        var options = {
            type: 'popup',
            responsive: true,
            innerScroll: true,
            modalClass: 'custom-popup-modal',

        };

        var popup = modal(options, $('#custom-popup-modal'));

        $( document ).ready(function() {
            $('#custom-popup-modal').modal('openModal');
        });     
    }
);
</script>


Answers 2


In your module layout file where you are called you template add cacheable="false" tag so you template content will not be cached .

<block class="Vendor\Module\Block\YourBlockFile" name="name of your block" template='your_template_file_name' cacheable="false"/>
Shashank Gupta
Shashank Gupta
January 12, 2018 09:01 AM

Add getCacheLifetime() in your block

For example this phtml file call in cms home page

{{block class="Vendor\Module\Block\Index" template="Vendor_Module::myfile.phtml"}}

In your block

<?php

namespace Vendor\Module\Block;

class Index extends \Magento\Framework\View\Element\Template
{

    public function __construct(
        ...
        \Magento\Framework\View\Element\Template\Context $context,
        ...
    ) {
        parent::__construct($context);
    }

    public function getCacheLifetime()
    {
        return null;
    }

}

If you call phtml file with layout xml you can disable cache by cacheable="false"

<block class="Vendor\Module\Block\Index" name="myblock" template="Vendor_Module::myfile.phtml" cacheable="false"/>
Prince Patel
Prince Patel
January 12, 2018 09:07 AM

Related Questions



Magento 2 add block phtml to every page

Updated September 04, 2017 05:09 AM

what is the block name for price block?

Updated April 26, 2015 21:04 PM

removing a template from the cache

Updated July 14, 2015 17:04 PM

Easy block updates for non-coders

Updated September 05, 2017 21:09 PM