Magento 2 Tutorials

How to Disable Custom Block Caching in Magento 2?

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading...Loading...
Disable Custom Block Caching in Magento 2

Today, we’re going to teach you guys how to disable custom block caching on a Magneto 2 product view page.

Caching is extremely helpful in improving the page load speed of a website. And as you might already know, Magento 2 based websites often take too much time to load, which is why caching in Magento 2 was introduced.

Now, when it comes to caching, its proper configuration is quite difficult. But if you want to achieve a perfect out, it’s important to do it right.

The good news is, it is not always necessary to customize the caching configuration for generating perfect output, sometimes all it takes is just disabling caching of certain blocks.

And in today’s post, we’re going to show you how to disable caching of custom blocks in Magento 2.

Method to Disable Custom Block Caching in Magento 2

There are two methods you can use to disable custom block caching in Magento 2.

Method #1

This is the easiest and simplest method to disable custom block caching out of the two methods. 

All you need to do is, open the layout from the backend of the product view page you want to disable custom block caching and set cacheable=”false”.

And that’s it! That’s all you need to do to disable custom block caching on a particular product view page in Magento 2.

Method #2

The second method is a bit complex compared to the first method, but it gets the job done.

All you need to do is, set _isScopePrivate property to “True” in the block class.

Once you enable that property, it will eliminate the aforementioned problem but will also achieve your goal.

Through this approach, you basically instruct a specific product view page’s cache module to replace the block content with a placeholder before caching. 

So, later when the page is loaded from the cache, your block will be rendered via AJAX request. 

Conclusion

And that’s about it!

This is how easy to disable custom block caching in Magento 2 product view page.

We hope that you found this tutorial helpful. If you have any questions or queries, please ask them in the comments below.

And if you need our professional assistance with Magento 2 development, feel free to contact us at any time.

You may also like
How to Add New Field at Order Total Block in Order PDF on Magento 2
How to Add New Field at Order Total Block in Order PDF on Magento 2?
Create Custom Product Attributes Magento 2
How to Create Custom Product Attributes in Magento 2?

Leave Your Comment

Your Comment*

Your Name*