Display number of Product in "My Cart" in header - Magento

Display number of Product in "My Cart" in header - Magento

August 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display number of Product in "My Cart" in header - Magento

Displaying Cart Product Count in Magento Header without Cache Dependence

In Magento e-commerce, displaying the number of products in a customer’s cart is a common requirement for enhancing user experience. While the default sidebar displays this information, we often need to show it prominently in the header as well.

A developer recently asked how to display the cart product count in the header and faced issues with cache dependence. In this post, I’ll explain how to achieve this without relying on caching mechanisms.

Understanding Magento’s Cart System

Magento uses a session-based system for handling customer carts. The shopping cart is stored in the user’s session, which stays active as long as they remain logged in or until it expires due to inactivity.

The checkout module provides methods like getSummaryCount() and getQuote()->getGrandTotal() to retrieve the number of items and total amount in a customer’s cart. You can use these methods within the header block to display real-time information.

Displaying Cart Count in the Header

To show the cart item count without cache dependence, you need to update the relevant header.phtml file directly. Here’s how:

  1. Navigate to your theme directory: Go to app/design/frontend/{your_package}/{your_theme}/template/page/html/.

  2. Open the ‘header.phtml’ file: In this file, find a section where you want to display the cart count, typically within a <ul> or <li> element.

  3. Add the following PHP code (replace {count} with your desired HTML tag):

{if $this->helper('checkout/cart')->getSummaryCount()}
    {count}{$this->helper('checkout/cart')->getSummaryCount()}
{else}
    <em>{$this->__('Empty Cart')}</em>
{/if}
  1. Save the changes: Make sure to upload this updated file back to your theme directory.

This code checks if there are any items in the customer’s cart. If so, it displays the total count using the getSummaryCount() method; otherwise, it shows “Empty Cart”.

Avoiding Cache Dependence

To avoid cache dependence, ensure that the header block is not cached. You can achieve this by adding an escape_html attribute to your <ul> or <li> element in the header.phtml file:

<ul class="nav">
    <li class="account-cart-item">{$this->helper('checkout/cart')->getSummaryCount()} Items</li>
</ul>

The escape_html attribute ensures that the output is not cached, allowing real-time updates to the cart count.

Conclusion

Displaying the number of products in a customer’s cart in the header can significantly enhance your store’s user experience. By following these steps and understanding Magento’s session-based cart system, you can easily implement this feature without relying on caching mechanisms.

Remember, keeping your theme files updated with the latest code is crucial for maintaining consistent behavior across your site. If you face any issues or need further assistance with customizing your Magento store, feel free to reach out to us at Laravel Company.

Happy coding!

Checkout Catalog Cache Extensions

Our network