Basically i want to show count on cart when no product is in cart.
-
Let me know if it'll not working for you– Ankita PatelNov 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– Niraj PatelSep 1, 2022 at 16:17
-
Yes it works for me– usmanJun 10 at 7:31
3 Answers
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/templates/cart/minicart.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; }
-
1
-
2<!-- ko if: getCartParam('summary_count') --> missing comma before summary_count Apr 9, 2021 at 12:22
-
1I 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 Mar 7, 2022 at 13:35
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!
-
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. Mar 7, 2022 at 13:31
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 -->´