I have implemented hole-punching of header in Magento, and even though I have got it working on a per-customer basis, I need the ability to take this one level deeper by making it work on diff cart items counts too.
Here is my code.
class AD_PageCache_Model_Container_Header extends Enterprise_PageCache_Model_Container_Abstract {
protected function _getIdentifier() {
return $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER, '');
}
// public function getCacheKeyInfo() {
// $info = parent::getCacheKeyInfo();
// die('boo');
// $info['cart_count'] = Mage::helper( 'checkout/cart' )->getCart()->getItemsCount();
// return $info;
// }
protected function _getCacheId() {
//return 'CONTAINER_HEADER_' . md5($this->_placeholder->getAttribute('cache_id') . $this->_placeholder->getAttribute('cart_count') ) . '_' . $this->_getIdentifier();
return 'CONTAINER_HEADER_' . md5( $this->_placeholder->getAttribute('cache_id') . $this->_getIdentifier() );
}
protected function _renderBlock() {
$blockClass = $this->_placeholder->getAttribute('block');
$template = $this->_placeholder->getAttribute('template');
$block = new $blockClass;
$block->setTemplate($template);
return $block->toHtml();
}
}
My understanding from what I have been reading from hole-punching threads on Magento is that the Mage app is not initialized when the FPC cache is serving the request, so basically, the method of adding a placeholder attribute can't work, since
Mage::helper( 'checkout/cart' )->getCart()->getItemsCount();
won't work, right?
And even though how is that it should be, but it didn't seem to run at all like I placed a die()
call in there but nothing happened.
So what I am missing? And how can I retrieve the cart items count so that it can be used to build the cache ID?
Progress: I found
Enterprise_PageCache_Model_Cookie::COOKIE_CART
but this changes only once upon cart updation. After that, it stays the same. This is weird, this feels like the solution but its behavior says otherwise.
I couldn't find cart items count in session either. So the only way I currently see to do this would be to save the cart quantity in session whenever it updates and then uses that in _getIdentifier()
.
I found that observers are inconsistent with the cart. In addition, updation the events are dispatched but for removal, it doesn't. So I guess I can add my observer to price updation of the quote somehow if that is consistent in having observers?
Also, I did read Full Page Cache invalidation on the cart (quote) change but it handles using
Enterprise_PageCache_Model_Cookie::COOKIE_CART
which doesn't work well in my case, although I think the problem lies inside it. What/How? I am not sure.
Also the current version of EE we have running doesn't even have Advanced folder under
Model/Container
. I am usingEE 1.10.1.1
Any suggestions?
_getIdentifier
so the block is unique to customers cart, or even the number of items in their cart?