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);
}
}
Best Answer
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