Search This Blog

Jun 13, 2011

Solved: Get Item Count in the Cart and Block HTML Output Caching

It is 100% fact that Magento is a monster e-commerce platform when it comes to caching functionalities. It cache collection data, html block, css, and more in order to make your site performance met your expectations.
But even you really need caching improve the performance of your site; there are some instances that you need to get rid this thing off. Functionalities that need to collect real time data. One example that exactly explains what I’m talking about is the CART ITEM COUNT. Take a look this screenshot below.


The item number must increases or decreases as time you add or remove item(s) in your cart. In order to do this you need the following code.

<?php
/**
 *
 * @see Mage_Checkout_Block_Cart_Count
 */
?>
<div class="cart-count-wrapper">
 <div class="cart-count">
  <?php $cart =  Mage::getModel('checkout/cart')->getQuote()->getItemsCount()?>
  <?php if ($cart == 0): ?>
   
    <div class="cart-empty">
    <a href="<?php echo $this->getUrl('checkout/cart')?>"><img src="https://www.wearpact.com/skin/frontend/default/wearpact/images/cart_icon.gif"/></a>
    <a href="<?php echo $this->getUrl('checkout/cart')?>"><span><?php echo $this->__('CART')." "."<strong>(".$cart.")</strong>" ?></span></a>
    </div> 
   <?php else: ?>
   <div class="cart-full">
   <a href="<?php echo $this->getUrl('checkout/cart')?>"><img src="https://www.wearpact.com/skin/frontend/default/wearpact/images/cart_icon_full.gif"/></a>
    <a href="<?php echo $this->getUrl('checkout/cart')?>"><span><?php echo $this->__('CART')." "."<strong>(".$cart.")</strong>" ?></span></a>
   </div> 
   <?php endif; ?>
  </div>
  <div class="checkout-cart"><a href="<?php echo $this->getUrl('checkout/cart')?>"><p><?php echo $this->__('CHECKOUT') ?></p> </a></div>
</div>

The lose end of the above code is that it isn’t work block HTML output caching is enabled. The ITEM COUNT will get the number of item added in the first time visit to the cart page. It is simply because that every block of the page you visited is cached by Magento. Therefore the next time you visit the page the cached page is rendered.

Disabling the BLOCK HTML Output cache is not the solution here. Since you will be losing sort of performance rather you go in making a copy a specific core model in local folder and make an edit to it. So here how you do it. In my given example, I do the following.

1. First we need to determine which directory CART ITEM COUNT code is included. In my given example I include the code in the top.phtml located in this directory.
app/design/frontend/default/wearpact/template/catalog/navigation/top.phtml
2. Since, the folder is in catalog/navigation. We need to copy the app/code/core/mage/Catalog/Block/Navigation.php to the local folder.
3. Now, after copying the file. Open the navigation.php in an editor then delete the code inside the function.
protected function _construct()
{
//delete the code inside this function.     
}
4. Upload and Clear Cache.

0 comments: