I have a custom module that displays data on my product pages. My module needs to get the current product id. I tried using:
Mage::registry('current_product');
This works on the first load but when I refresh, current_product no longer has data with full page caching on.
Any ideas?
The catalog product action controller isn't dispatched, when the full page cache handles the request (which makes sense to keep things fast). So, the registry variables are never set. I assume you are generating the block dynamically on the otherwise fully cached page. My recommendation is to try to avoid expensive loads, since that would undermine the speed improvements by the full page cache. You really want to cache the block if at all possible, even if it's a separate cache entry for each customer and each product.
That said, here is how to do that:
In the container, implement the
_getIdentifier()
method:Also expand the
_getCacheId()
method to include the return value of the method _getIdentifier() and a new placeholder attribute: product_idNext, in the block class, extend the method
getCacheKeyInfo()
. All entries in the cache_key_info array with a string index are set on the placeholder as attributes. That is how we can pass the product id to the placeholder.Then enable the method
_saveCache()
by not overriding it in your container class and returningfalse
. So now, because the container returns a valid id from_getCacheId()
, and_saveCache()
is inherited from the parent class, the the block can be cached, and applied to the content in an efficient manner inEnterprise_PageCache_Model_Container_Abstract::applyWithoutApp()
.You can set the lifetime of the cache entry by having your container extend from
Enterprise_PageCache_Model_Container_Customer
instead ofEnterprise_PageCache_Model_Container_Abstract
.If you still need to pass the product_id to the block (even though it's cached now), you can do so in the
_renderBlock()
method of your container:you should have in the controller of your module something which init the registration of the current product. You will need to post between the page visited by a user the id of your product (recommended) or save it in a session. Here is an example of what you can do in a controller: