Magento Enterprise – How to Hole Punch a Child Block in Catalog/Product/List Block

ee-1.11full-page-cacheholepunchingmagento-enterprise

Scenario:

I have category landing page, in which another vendor has customized to add "Purchased" text instead of "Add to cart" in case the user has purchased it, hence this will be different for each user.

All iz well till now, until we turned on "The" FPC.

While I understand Holepunching a little better now I had this plan:

My Plan:

  1. We keep rest code in list.phtml as the same.
  2. I just cut the logic of the showing of the "Add To Cart" or "Purchased" and put it in another phtml.
  3. I would add this new block as the child of catalog/product/list block in the catalog.xml with this new phtml.
  4. Have a container model responsible for the _renderBlock. We say _saveCache false for this block and it would not be cached.

Question / Difficulties

  • I am not really aware of the exact implementation for this plan.
  • For step 4 what should be the implementation of _renderBlock like ?
  • How whould I get the productobject inside this phtml ? as The list.phtml works on the productCollection and we just loop over this and get product object. now, how does one get the project object now in the child block ?
  • I need a few pointers on how this can be done best. while I read about a product view page holepunch method I am not exactly sure how to do this for category page.
  • If nothing works I just want FPC to not cache the list block at all , but again not sure how to disable caching for only this block ?

Best Answer

So the real question is - is the output of this "Purchased" text coming from a block? Or are you incorporating it in the phtml as logic?

The answer should be that it's coming from a Block. The reasoning is that Magento implements the hole punch via the block output. To do this is very simple:

  • Create a cache.xml file in the etc directory of your module
  • Create the page cachebust Container block

etc/cache.xml

<?xml version="1.0"?>
<config>
    <placeholders>
        <purchased_button>
            <block>purchased/button</block>
            <template>checkout/button.phtml</template>
            <name>product.info.addtocart</name><!-- THIS MUST MATCH THE ALIAS IN THE LAYOUT XML EXACTLY -->
            <placeholder>PURCHASED_BUTTON_TEXT</placeholder>
            <container>YourCompany_YourModule_Model_Container_Purchased</container>
            <cache_lifetime>86400</cache_lifetime>
        </purchased_button>
    </placeholders>
</config>

Model/Container/Purchased.php

<?php

class YourModule_YourCompany_Model_Container_Purchased extends Enterprise_PageCache_Model_Container_Abstract
{

    protected function _getCacheId()
    {
    return 'PURCHASED_BUTTON_TEXT_' . md5($_COOKIE["frontend"]);
    }

    public function applyInApp(&$content)
    {
        $block = $this->_getPlaceHolderBlock();

        $blockContent = $block->toHtml();

        $this->_applyToContent($content, $blockContent);

        return true;
    }

    protected function _saveCache($data, $id, $tags = array(), $lifetime = null)
    {
        return false;
    }
}
Related Topic