2

I am trying to display the number of items currently in my cart, however not getting any result. In magento 1 we just just add the following to our phtml file and it will just echo the no. of items in cart.

Mage::getModel(‘checkout/cart’)->getQuote()->getItemsCount();

This of course does not work in in magento 2 (version 2.0.2). Within my custom html file. I just need to echo just the number, just like you get when get a number on the mini cart. This is the section of code i need to put it in

<span class="cart-text"><strong>Basket:</strong>
    <span class="cart-count">??????</span> item(s)
</span>

I need to put the number in replace of the question marks. This may be a simple question, however i am still in the early stages of my learning curve, help is greatly appreciated. Thanks

8

try this

$helper = $this->helper('\Magento\Checkout\Helper\Cart');
 echo $helper->getItemsCount();
2
  • That works exactly as i needed it. Thank you @Qaisar Satti
    – AlphaOne
    Mar 29 '16 at 9:21
  • 1
    It's not working if caches are enabled.
    – Vish V
    Jun 15 '18 at 12:20

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.