Magento 2: how to hide product from minicart

by Kristjan O.   Last Updated August 10, 2018 12:09 PM

In our store we're offering a free gift that is attached to the product customers can buy, but since they clutter the mini cart I came to the idea of simply hiding those products in it (and still keeping them in cart). My solution was to copy and edit the /vendor/magento/module-checkout/view/frontend/web/js/view/minicart.js and update the getCartItems() and getCartLineItemsCount() and simply splice out those products. But, some products are removed from frontend, while others are not and summary_count is not properly updated.

Updated getCartItems():

getCartItems: function () {
    var items = this.getCartParam('items') || [];
    if (items.length) {
        for(var i = 0; i < items.length; i++){
            if (items[i].product_price_value === 0) {
                items.splice(i,1);
            }
        }
    }

    items = items.slice(parseInt(-this.maxItemsToDisplay, 10));

    this.update(items);

    return items;
},

Updated getCartLineItemsCount():

getCartLineItemsCount: function () {
    var items = this.getCartParam('items') || [];

    if (items.length) {
        for(var i = 0; i < items.length; i++){
            if (items[i].product_price_value === 0) {
                items.splice(i,1);
            }
        }
    }

    return parseInt(items.length, 10);
}

Knockout template (wasn't changed) looks OK and above should work. Is there a better method of doing what I want? Note that setting the Visibility to Not visible individually does not work, because it also hides the product which can be bought separately. There's not an option to keep two different (same) products for this.

Magento version is 2.2.3.

EDIT: Forgot to mention, for adding a "free gift/product", we're using Amasty Free Gift plugin.



Related Questions




Apply cart rule discount in mini cart

Updated March 05, 2018 13:09 PM