1

When I put the code to show the number of items in the cart in my custom headerfile.phtml it doesn't work, but when I access a checkout page it works perfectly, what to do?

Here's the code:

<?php
$cart = $this->helper('\Magento\Checkout\Helper\Cart');
$itens = $cart->getQuote()->getItemsQty();
$itens = round($itens, 0);
if ($itens > 0)
echo $itens;
else 
echo 0;
?>
1
  • It will not work if the header is cached.
    – Nits
    Nov 24 at 15:24
0

I would suggest, you should take a reference of the below file. Here you can see all require variables and use them in your custom phtml file vendor/magento/module-checkout/view/frontend/templates/cart/minicart.phtml:23

<!-- ko if: getCartParam('summary_count') -->
                <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
            <!-- /ko -->

Please let me know if you this will not help.

2
  • put this code in my custom phtml and it didn't have any effect, is there something missing?
    – lucas
    2 days ago
  • Copying this code will not work, you have to understand the logic that how it is implemented in the core mini cart.phtml file. I have provided references.
    – Nits
    yesterday

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.