0

I want to add a class in parent div if minicart/cart is empty and remove same class if minicart/cart has an item(s).I made changes in minicart.phtml. below is my code for that.

<div class="block block-minicart"
data-bind="css: { emptyBag: !!getCartParam('summary_count') == false }
                 data-role="dropdownDialog"
                 data-mage-init='{"dropdownDialog":{
                    "appendTo":"[data-block=minicart]",
                    "triggerTarget":".showcart",
                    "timeout": "2000",
                    "closeOnMouseLeave": false,
                    "closeOnEscape": true,
                    "triggerClass":"active",
                    "parentClass":"active",
                    "buttons":[]}}'>

I have added data-bind="css: { emptyBag: !!getCartParam('summary_count') == false }" peace of code to check if cart is empty or not. but when i check console it's giving me an error of getCartParam is not defined. Please help, I have worked on magento but new in KO so facing problem in that

1 Answer 1

1

You cannot add the css to this div because it was not init under scope: 'minicart_content'

So a work around as you can add the class to the div id minicart-content-wrapper and add the scope minicart_content to the div class block block-minicart

<?php if ($block->getIsNeedToDisplaySideBar()): ?>
    <div class="block block-minicart"
         data-bind="scope: 'minicart_content'"
         data-role="dropdownDialog"
         data-mage-init='{"dropdownDialog":{
            "appendTo":"[data-block=minicart]",
            "triggerTarget":".showcart",
            "timeout": "2000",
            "closeOnMouseLeave": false,
            "closeOnEscape": true,
            "triggerClass":"active",
            "parentClass":"active",
            "buttons":[]}}'>
        <div id="minicart-content-wrapper" data-bind="css: { emptyBag: !!getCartParam('summary_count') == false }, scope: 'minicart_content'">
            <!-- ko template: getTemplate() --><!-- /ko -->
        </div>
        <?= $block->getChildHtml('minicart.addons') ?>
    </div>
<?php endif ?>

Hope it help

5
  • yes i got that but is this possible to add class in block block-minicart div..? Dec 6, 2018 at 9:11
  • i got the solution, but thanks for your help i really appreciate Dec 6, 2018 at 9:25
  • @KaranAdhikari Can you please tell me which solution is work for you ? Jan 26, 2019 at 11:44
  • @KaranAdhikari can you please share your solution here? Mar 24, 2021 at 10:16
  • @SanjayGohil can you please share the solution if you found it? Mar 24, 2021 at 10:19

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.