I have implemented holepunching 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 item 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 holepunching threads on Magento is that Mage app is not initialized when 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 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 use that in _getIdentifier()
.
I found that observers are inconsistent for cart. For 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?
Any suggestions?
getCacheKeyInfo()
method and then in_getCacheId()
of your placeholder class build unique key (i.e. container individual cache id). – Dmytro Zavalkin