1

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.

  • If you add the product into cart programmatically, the product will not appear in the mini cart until you add your custom controller action in section.xml under etc folder. – Dinesh Yadav Aug 10 '18 at 11:52
  • Forgot to mention, we're using a plugin to add a free gift. – Kristjan O. Aug 10 '18 at 11:56
  • Use the custom controller to add the product. It will add the product to cart but will not display in a mini cart until you create a section for this. – Dinesh Yadav Aug 10 '18 at 13:12
  • Not sure if that's an option (I'm still beginner in Magento) since free gift is added to some products and not the others. This is configurable from admin and I am afraid that going the route you're suggesting, we will loose this configurability. – Kristjan O. Aug 10 '18 at 14:01
0

Easy way to do this is just add display:none; to the "gift" product selector. And continue on with your life ;P

  • Product selector? Gift is added automatically based on product added to cart. – Kristjan O. Aug 10 '18 at 14:01
  • I know but just look in the DOM and see if you can target it with css. It probably has a unique selector or path to select on the gift items. – Jordan Aug 13 '18 at 8:56
  • Unfortunately not, did consider this option before. But even if it did, then the cart quantity would be off and I would prefer to update that one as well. – Kristjan O. Aug 13 '18 at 11:40

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.