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, 2021 at 15:24

2 Answers 2

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
    Nov 25, 2021 at 13:13
  • 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
    Nov 26, 2021 at 5:34
0
<?php
$cart = $this->helper('\Magento\Checkout\Helper\Cart');
$itens = $cart->getQuote()->getItemsQty();
$itens = round($itens, 0);
if ($itens > 0)
echo $itens;
else 
echo 0;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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