2

I want to disable cache only for particular block which is called in some pages like Home Page, Category Listing page , Product page and Shopping cart page etc.

<block class="Namespace\Module\Block\Custom" name="custom.storetime" template="Namespace_Module::storetime.phtml"/>

and I do not want to use cacheable=false as its disable the cache for entire page.

2

I have solved my problem using ajax post request on phtml file

So I have created two phtml files and in my first phtml file which show the output data on page reload I have called the ajax post request to my custom controller

<div id="customdata"></div>
<?php
$ajaxurl = $block->getAjaxUrl();
?>
<script type="text/x-magento-init">
        {
            "*": {
                "Mymodule_Custom/js/custom": {
                    "AjaxUrl": "<?php echo $ajaxurl; ?>",
                    "CurrentProduct": "<?php echo $currentProductId; ?>",
                }
            }
        }
</script>

and then in controller called another phtml file and load content from that phtml file using below type of code.

$result = $this->_resultJsonFactory->create();
$resultPage = $this->_resultPageFactory->create();
$currentProductId = $this->getRequest()->getParam('currentproduct');


$data = array('currentproductid'=>$currentProductId);

$block = $resultPage->getLayout()
        ->createBlock('Mymodule\Custom\Block\Index\View')
        ->setTemplate('Mymodule_Custom::customview.phtml')
        ->setData('data',$data)
        ->toHtml();

$result->setData(['output' => $block]);
return $result;

and in this customview.phtml I have loaded my dynamic content and then set to first phtml file using ajax response and it work fine with cache

<?php
$productData = $block->getData();
$productId = $productData['data']['currentproductid'];
$products = $block->getProducts($productId);
?>
<div>
  <div>YOUR HTML</div>
</div> 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.