How do I include a dynamic block in the product page with full page caching turned on?

How do I include a dynamic block in the product page with full page caching turned on?

September 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I include a dynamic block in the product page with full page caching turned on?

Dynamic Blocks on Full Page Cache: A Comprehensive Guide

As an e-commerce developer, you often face challenges when trying to implement dynamic elements on pages that have full page caching enabled. This is particularly true if you need to display different content based on the logged-in user’s account or product-specific information.

In your case, you’ve encountered a common issue where the cached version of one product page is displayed for all subsequent pages due to the shared block cache ID. To resolve this, we’ll explore how to create a custom page cache class that generates unique cache IDs per product and prevents caching entirely when needed.

Analyzing Your Current Setup

Your current approach uses Magento Page Cache and includes the necessary XML files in app/code/local/MyCompany/MyModule/PageCache. However, let’s examine some areas where your implementation can be improved to ensure proper cache behavior.

The _getCacheId() Function

The _getCacheId() function is correctly using a unique identifier derived from the product ID (cache_id attribute). This ensures that each product page generates a distinct cache ID. However, you should consider adding additional identifiers to cater for other dynamic elements like user sessions or special promotions.

The _saveCache() Function

This function is set to return false, which effectively disables caching. While this prevents the cached version of one product from being displayed on subsequent pages with a different ID, it also means that no cache will be generated at all for any product page. This could lead to performance issues if your site has a large number of products.

The _renderBlock() Function

The _renderBlock() function is correctly rendering the specified block using the provided template and layout. However, this approach assumes that the dynamic content is included within the block’s template file, which may not always be the case.

A Better Approach for Dynamic Content

To achieve your goal of displaying different information per product based on user accounts without caching issues, consider the following improvements:

  1. Separate Dynamic and Static Content: Divide your product page into static sections (which can be cached) and dynamic sections (which should not be cached). You can use Magento’s layout XML files to manage which sections are cached and which aren’t.

  2. Use Magento Blocks for Dynamic Content: Create separate blocks for the dynamic content you want to display on each product page. These blocks can be assigned to a specific product or user group, ensuring that the correct information is displayed.

  3. Implement Varnish or Opcode Cache: If Magento Page Cache is causing issues due to its aggressive caching strategies, consider using more advanced cache solutions like Varnish or Opcode Cache (for PHP). These can handle complex caching rules and provide better control over which data should be cached.

  4. Leverage Magento’s Event System: You can hook into Magento events like catalog_product_get_final_price to add dynamic pricing, or use controllers with no view layout assigned for special content.

Real-World Example: Dynamic Blocks Without Caching

Let’s examine a simple example of creating a block that displays different content per user session without caching:

  1. Create the Custom Block: Create a new block in your custom module’s Block directory, e.g., MyCompany_PageCache_Block_Dynamic.php:
class MyCompany_PageCache_Block_Dynamic extends Mage_Core_Block_Template
{
    public function getDynamicContent()
    {
        $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
        // Add your dynamic content generation logic here, e.g., fetch data from a database or an API.
        return 'Welcome User #'.$customerId;
    }
}
  1. Assign the Block to the Product Page: In your product page layout XML file (usually app/design/frontend/base/default/layout/catalog.xml), add the following:
<block type="mycompany_pagecache/dynamic" name="dynamic_content_block" template="mycompany/pagecache/dynamic.phtml"/>
  1. Create the Template File: Create a new PHTML template file at app/design/frontend/default/mycompany/template/pagecache/dynamic.phtml with your dynamic content:
<div class="dynamic-content">
    <?php echo $this->getDynamicContent(); ?>
</div>
  1. Configure the Cache Settings: Configure your cache settings in your custom module’s PageCache/etc/config.xml file to ensure that this specific block is not cached by adding a <cache> node with the appropriate attributes.

Conclusion

In conclusion, managing dynamic content on pages with full page caching enabled requires careful consideration of Magento’s caching mechanisms and the separation of static and dynamic elements. By leveraging Magento’s built-in features like blocks, layouts, and events, you can achieve your goal without compromising performance or user experience.

Remember to thoroughly test your implementation in a staging environment before deploying it to production. Also, keep an eye on your server logs for any potential issues related to caching or memory management.

For more advanced caching techniques and performance optimization strategies, consider exploring the resources available at LaravelCompany.com. They provide comprehensive guides and tutorials tailored to Magento developers looking to improve their e-commerce platforms’ speed and efficiency.

Catalog Cache Extensions Customers

Our network