4

Basically i want to show count on cart when no product is in cart.

enter image description here

3
  • Let me know if it'll not working for you Commented Nov 13, 2019 at 7:13
  • @usman please acknowledge the answer,if it is not working please comment it because here all community members do hard work to post answers. Your responsibility to response if anybody answer it Commented Sep 1, 2022 at 16:17
  • Yes it works for me
    – usman
    Commented Jun 10, 2023 at 7:31

3 Answers 3

8

You need to override minicart.phtml in your theme on below path :

Copy From

vendor/magento/module-checkout/view/frontend/templates/cart/minicart.phtml

Paste To

app/design/frontend/{Vendor}/{themename}/Magento_Checkout/te‌​mplates/cart/minicar‌​t.phtml

Now, replace below code.

From

<span class="counter-number"><!-- ko text: getCartParam('summary_count') --><!-- /ko --></span>

To

<!-- ko if: getCartParam('summary_count') -->
   <span class="counter-number"><!-- ko text: cart().summary_count --><!-- /ko --></span>
<!-- /ko -->
<!-- ko ifnot: getCartParam('summary_count') -->
   <span class="counter-number">0</span>
<!-- /ko -->

Hope, It will helpful for you.

Also update CSS: .minicart-wrapper .action.showcart .counter.qty.empty { display: inline-block; }

3
  • 1
    The right way to do it. +1 for the answer.
    – Sumit
    Commented Nov 13, 2019 at 6:54
  • 2
    <!-- ko if: getCartParam('summary_count') --> missing comma before summary_count
    – Nirav Bhoi
    Commented Apr 9, 2021 at 12:22
  • 1
    I think @NiravBhoi has right! The quoting <!-- ko if: getCartParam(summary_count') --> is wrong!!!! It should be <!-- ko if: getCartParam('summary_count') --> Otherwise the rendering of the minicart does not work properly! But this solution is not working anymore on M2.4.2 Commented Mar 7, 2022 at 13:35
1

You can add below line in your CSS file

.counter.empty{display: block !important;}

and run below commands

php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush

Hope this will work for you!

1
  • This is not working when u access the page with a cleaed cache. It renders nothing initally. After u add a product and remove it again from the cart it shows u the 0. But this is not the requirement here. Commented Mar 7, 2022 at 13:31
1

The answer above is old and not working for M2.4.2 although the file replacements are correct but the code replacement is wrong.

From

    <span class="counter-number"><!-- ko text: getCartParam('summary_count') --><!-- /ko --></span>

To

    <!-- ko if: getCartParam('summary_count') -->
            <span class="counter-number"><!-- ko text: getCartParam('summary_count') --><!-- /ko --></span>
    <!-- /ko -->
    <!-- ko ifnot: getCartParam('summary_count') -->
            <span class="counter-number">0</span>
    <!-- /ko -->´

Your Answer

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

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