1

I've added a custom cart count in header on my Magento 2 theme and the count is only displaying correctly on checkout pages. Any idea how to resolve so it displays on all pages correctly?

I'm calling in a static block like so:

<li class="link-cart">
     <a href="{{store url="checkout/cart"}}">
          <i class="fa fa-shopping-cart" aria-hidden="true"></i> 
          My Cart {{block class="Magento\Checkout\Block\Cart\Sidebar" name="menu.minicart" template="Magento_Theme::additional/header/menu-cart.phtml"}}
      </a>
</li>

And the menu-cart.phtml file:

<?php
$counter = $this->helper('\Magento\Checkout\Helper\Cart');
if ($counter && $counter->getSummaryCount()) {
    echo '(' . $counter->getSummaryCount() . ')';
}
1

Assuming you have full page cache enabled, the solution you're trying to implement will not work. Most of the pages (such as homepage, category page, product page) are cached, including the header cart block. The checkout page however is not cached and will always do all the processing on all the blocks.

This means that you have to use Knockout JS to show the cart content and count the items by loading the information from local storage.

Here's an example of the KO template:

<span class="counter total-qty empty"
      data-bind="css: { empty: !!getCartParam('summary_count') == false }, blockLoader: isLoading">
    <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
</span>

you can find Magento's default templates in the checkout module: vendor/magento/module-checkout/view/frontend/web/template/sidebar.html and vendor/magento/module-checkout/view/frontend/web/template/minicart/content.html

Hope this helps

4
  • when disable cache it is showing but without disable how it is possible ? Apr 10 '19 at 11:16
  • @sumeetbajaj : have you achive that with enabled cache. Jan 20 '20 at 22:04
  • @BalwantSingh i tried with other method Jan 21 '20 at 5:23
  • @sumeetbajaj : Which method? actually I am facing the same issue and looking for soln but not get any success. Can you please answer that method here. Jan 21 '20 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.