2

In Magento 2, I have made a special system. When a (configurable or virtual) product is ordered, the related products are automatically added.

The configurable and virtual products are mealboxes, and the related products that come with the box are the recipes. They are free of charge, and are added so the customer can 'save' the recipes in the customer dashboard that way.

Enough of that, now my question:

In the minicart, I want to filter the products. I want to show every product type EXCEPT for the downloadable products. But I don't know how to filter this, since the products are loaded via knockout.js...

Is there a way to do this?

  • You're trying to remove the downloadable products from mini cart? – Khoa TruongDinh May 8 '17 at 13:55
5
+50

We can get the cart items from local storage: localStorage.getItem('mage-cache-storage'). We can see the type of product:

enter image description here

See more here: Getting the product SKU in the header mini-cart

We need to override the html template: vendor/magento/module-checkout/view/frontend/web/template/minicart/content.html in your custom theme or module.

For example, in your custom module:

view/frontend/requirejs-config.js

var config = {
    map: {
        '*': {
            'Magento_Checkout/template/minicart/content.html':
                'Vendor_Module/template/minicart/content.html'
        }
    }
};

Go to line 65 ~ 75, add the condition in the loop: <!-- ko if: item.product_type !== 'downloadable' -->

<!-- ko if: getCartParam('summary_count') -->
    <strong class="subtitle"><!-- ko i18n: 'Recently added item(s)' --><!-- /ko --></strong>
    <div data-action="scroll" class="minicart-items-wrapper">
        <div id="mini-cart" class="minicart-items" data-bind="foreach: { data: getCartParam('items'), as: 'item' }">
            <!-- ko if: item.product_type !== 'downloadable' -->
                <!-- ko foreach: $parent.getRegion($parent.getItemRenderer(item.product_type)) -->
                    <!-- ko template: {name: getTemplate(), data: item, afterRender: function() {$parents[1].initSidebar()}} --><!-- /ko -->
                <!-- /ko -->
            <!-- /ko -->
        </div>
    </div>
<!-- /ko -->

In your custom theme, we don't need to create require config file. Copy the content file to your new theme. For example: app/design/frontend/Vendor/Theme/Magento_Checkout/web/templa‌​te/minicart/content.‌​html

[I have a note]: we need to change the items count on mini cart also.

| improve this answer | |
  • Stupid question, but where exactly do I add the requirejs-config.js file in my own theme? :p – Maarten Wolfsen May 9 '17 at 10:59
  • In your custom theme, you don't need to use requirejs config. – Khoa TruongDinh May 9 '17 at 11:14
  • In your custom theme, copy the content file to your new theme. For example: app/design/frontend/Vendor/Theme/Magento_Checkout/web/template/minicart/content.html – Khoa TruongDinh May 9 '17 at 11:26

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.