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.

Rakesh Donga
5,4342 gold badges26 silver badges58 bronze badges
asked Jan 2, 2020 at 17:36
1

1 Answer 1

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> 
answered Jan 28, 2020 at 18:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.