1

I Managed to create a custom block which displays random data like rime and letters/numbers

However that was the test to understand the procedure to create a block in Magento.

Then I tried to switch from that block, created another block where instead of extending from Mage_Core_Block_Template I changed to use Enterprise_TargetRule_Block_Catalog_Product_List_Related since my objective is to have related products to be dynamic, I pointed my cache.xml to this new block.

I placed a Mage::log in related list block class to know when this is called, I can see it is called once then is cached.

For the life of me I can't make it work, this is my code please advise.

Note: The rewrite has another separate purpose, and actually my log code line is set in this portion under method: public function getProductIds($object)

Lg/Hole/etc/config.xml

<config>
    <modules>
        <Lg_Hole>
            <version>0.0.1</version>
        </Lg_Hole>
    </modules>

    <global>
        <blocks>
            <lg_hole>
                <class>Lg_Hole_Block</class>
            </lg_hole>
        </blocks>
        <models>
            <lg_hole>
                <class>Lg_Hole_Model</class>
            </lg_hole>
            <enterprise_targetrule_resource>
                <rewrite>
                    <index>Lg_Hole_Model_Mymodel</index>
                </rewrite>
            </enterprise_targetrule_resource>
        </models>
    </global>
</config>

Lg/Hole/etc/cache.xml

<config>
    <placeholders>
        <lg_hole>
            <block>lg_hole/related</block>
            <name>hole_view_example</name>
            <placeholder>CACHE_TEST</placeholder>
            <container>Lg_Hole_Model_Cachetest</container>
            <cache_lifetime>86400</cache_lifetime>
        </lg_hole>
    </placeholders>
</config>

Lg/Hole/Model/Cachetest.php

class Lg_Hole_Model_Cachetest extends
Enterprise_PageCache_Model_Container_Abstract {
protected function _getCacheId()
{
    return 'HOMEPAGE_PRODUCTS' . md5($this->_placeholder->getAttribute('cache_id'));
}
protected function _renderBlock()
{
    $blockClass = $this->_placeholder->getAttribute('block');
    $template = $this->_placeholder->getAttribute('template');

    $block = new $blockClass;
    $block->setTemplate($template);
    return $block->toHtml();
}

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

public function applyWithoutApp(&$content)
{
    return false;
}
}

Lg/Hole/Block/Related.php

class Lg_Hole_Block_Related extends Enterprise_TargetRule_Block_Catalog_Product_List_Related
{

}

1 Answers1

0

Do you have any need of extending from Enterprise_TargetRule_Block_Catalog_Product_List_Related ?

If yes : do you understand that this block contains its own logic ? You must have a loaded product in the registry (@see Enterprise_TargetRule_Block_Catalog_Product_List_Abstract::getProduct() like on a product page) and have product links (type related) on this product.

If you want to make a completely standalone product list : a simple class like Mage_Catalog_BLock_Product_List would be enough because it's the rawest block from a list of product (no specific logic added)

or better .. user a widget

Jscti
  • 14,096
  • 4
  • 62
  • 87
  • Thanks for answering, actually this is the normal related list on a product detail page, so not really trying to reinvent the wheel, I just extended thinking it was probably easier to have the hole punched block in my own Module rather than punching a hole pointing to a core/enterprise file, if no need to extend I understand, I just need this normal "Related Items Block" to be out of cache at all times. In regards of understanding this block contains its own logic, yes I do & actually had to implement some conditions for the project I'm working on (which leads me to need it out of cache) – Ezri Hiccup Oct 22 '12 at 16:05
  • Okay, if you really need to get it away from the cache a simple `return false` in the `_saveCache()` method of your container should work – Jscti Oct 23 '12 at 08:15
  • Did you try without FPC activated ? are you sure your block is OK without it ? – Jscti Oct 23 '12 at 08:23
  • 1) I thought the return false would do, turns out it didnt it keeps caching it 2) I tried with FPC off and it works well just as I need to when FPC is on, I know because I placed a log on **public function getProductIds($object)** , when FPC is off -> Log is triggered with every request when FPC is on -> Triggers only once – Ezri Hiccup Oct 23 '12 at 19:05
  • Ok so the problem is clearly on the Container. First are you sure the container is called (put a log in it) ? Second follow this link http://stackoverflow.com/questions/11745877/how-to-deal-with-the-messages-block-in-magento-hole-punching?rq=1 – Jscti Oct 23 '12 at 19:36
  • I have tracked down this block to be this specific type of block with this name: **Type: enterprise_targetrule/catalog_product_list_related**............. **Name: catalog.product.related** But, I dont really think it's being called properly, where do you suggest I can place the log file? – Ezri Hiccup Oct 23 '12 at 23:14
  • add some Mage::log() in your _renderBlock et saveCache method in the Container – Jscti Oct 24 '12 at 07:47
  • Ok Sorry to take that long to reply into this thread... Finally I used the Mage::log sugestion and noticed it was not being triggered, so after several attempts I gave up and decided to take a look to it in a different way.::::::::: So, I noticed the related products (the products and not the container) are already under a hole, so I studied the app/code/core/enterprise/PageCache/cache.xml file and tracked the flow, noticed then this method was actually calling the parent container, and not as I was thinking the container calling the children! – Ezri Hiccup Nov 20 '12 at 00:36
  • So now it was easy to create my own cache.xml at my own module, I used the same placeholder, but this time I extended the Model class already created at PageCache and overrode the SaveInfoCache method to return false, this keeps the collection from going into cache everytime!!! – Ezri Hiccup Nov 20 '12 at 00:41