Question

Intially when the cache is cleared and I visit a page the block and corresponding template load perfectly. Then, when I revisit this page the block isn't loaded at all. Am I missing something in this hole punching process?

Model Container:

class Myname_Page_Model_Container_Ajaxcart extends Enterprise_PageCache_Model_Container_Abstract
{
    protected function _renderBlock()
    {
        $blockClass = $this->_placeholder->getAttribute('block');
        $template = $this->_placeholder->getAttribute('template');

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

Cache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <placeholders>
        <ajax_cart>
            <block>page/html_ajaxcart</block>
            <name>ajaxCart</name>
            <placeholder>AJAX_CART</placeholder>
            <container>Myname_Page_Model_Container_Ajaxcart</container>
            <cache_lifetime></cache_lifetime>
        </ajax_cart>
    </placeholders>
</config>

layout.xml:

<block type="page/html_ajaxcart" name="ajaxCart" as="ajaxCart" template="page/html/ajaxCart.phtml"/>

template.phtml:

<?php echo $this->getChildHtml('ajaxCart');?>

config file:

<?xml version="1.0"?>
<config>
    <modules>
        <Myname_Page>
            <version>0.1.0</version>
        </Myname_Page>
    </modules>
    <frontend>
        <routers>
            <page>
                <use>standard</use>
                <args>
                    <module>Myname_Page</module>
                    <frontName>page</frontName>
                </args>
            </page>
        </routers>
    </frontend>
    <global>
        <blocks>
            <page>
                <rewrite>
                    <html_ajaxcart>Myname_Page_Block_Html_Ajaxcart</html_ajaxcart>
                </rewrite>
            </page>
            <myname_page>
                <class>Myname_Page_Block</class>
            </myname_page>
        </blocks>
    </global>
</config>

block file:

<?php
class Myname_Page_Block_Html_Ajaxcart extends Mage_Core_Block_Template
{
    // sort quote by item date of addition
    public function sortByUpdatedAt($quote)
    {
        // initialize item collection
        $items = array();
        $collection = $quote->getAllVisibleItems();

        // get updated at date for each item
        foreach ($collection as $item) {
            if (!$item->isDeleted()) {
                array_push($items, $item->getData('updated_at'));
            }
        }


        // sort and keep key association
        asort($items);

        // reverse order and keep key association
        $k = array_reverse(array_keys($items));
        $reversed = array_reverse(array_values($items));
        $items = array_combine($k, $reversed);

        // collection is sorted
        $collection = $this->finishSort($items, $collection);
        return $collection;
    }

    // sort collection by established ordering
    protected function finishSort($items, $collection)
    {
        $sortedCollection = array();

        // collection will reflect sorted item array
        foreach ($items as $key => $value) {
            // get new order by established key value order
            array_push($sortedCollection, $collection[$key]);
        }
        return $sortedCollection;
    }

    public function getProductPrice($product)
    {
        // return formatted special price
        if ($product->getSpecialPrice() != null && Mage::app()->getLocale()->isStoreDateInInterval(Mage::app()->getStore(), $product->getSpecialFromdate(), $product->getSpecialToDate())) {
            $price = $product->getSpecialPrice();
        } else {
            $price = $product->getPrice();
        }
        // return formatted price
        return Mage::helper('core')->formatPrice($price, true);
    }
}
Was it helpful?

Solution

I believe you are missing a implementation of _getCacheId & _saveCache

A cache_id is already defined if none is provided but is of anonymous naming convention. Define an ID for the block and reference it in your _getCacheId

Add a etc/cache.xml file with a <config> root to your module. (see Enterprise/PageCache/etc/cache.xml). Choose a unique [placeholder] name.

The placeholders/[placeholder]/block node value must match the class-id of your custom dynamic block, e.g. mymodule/custom

The placeholders/[placeholder]/container node value is the class to generate the content dynamically and handle block level caching

The placeholders/[placeholder]/placeholder node value is a unique string to mark the dynamic parts in the cached page

placeholders/[placeholder]/cache_lifetime use to be ignored, but not anymore. For older instances specify a block cache lifetime in the container’s _saveCache() method instead.

Implement the container class and extends Enterprise_PageCache_Model_Container_Abstract. Use _renderBlock() to return the dynamic content.

Implement the _getCacheId() method to return a unique string. Use cookie values instead of model ids (lower cost).

Implement _saveCache() to simply return, or pass a cache lifetime value to the parent method. Voila, dynamic block done.

One last note: You DON’T have the full Magento App at your disposal when _renderBlock() is called. Be as conservative as possible.

OTHER TIPS

I think you have missed the <template> tag in the cache.xml file. But 'm not sure about this.

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <placeholders>
        <ajax_cart>
            <block>page/html_ajaxcart</block>
            <name>ajaxCart</name>
            <placeholder>AJAX_CART</placeholder>
            <template>page/html/ajaxCart.phtml</template> <!-- Your template file -->
            <container>Myname_Page_Model_Container_Ajaxcart</container>
            <cache_lifetime>false</cache_lifetime>
        </ajax_cart>
    </placeholders>
</config>

Try the above code it may work.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top