Question

getCartItems: function () {
                var items = this.getCartParam('items') || [];
                _.each(items, function (item, key) {
                    console.log(item.product_id);
                 // i have 1,2,3 product ids and need to add a check with minicart product ids and if 
                   exists then show custom popup.
                }, this);
                items = items.slice(parseInt(-this.maxItemsToDisplay, 10));
                return items;
            }

Anyone have any idea for this?

Thanks

Was it helpful?

Solution

You can use contains function of underscore js to check in array items. And after use Magento_Ui/js/modal/modal to show popup based on condition.

var myItems = ['1', '2', '3', '5'];
_.each(items, function (item, key) {
    if(_.contains(myItems, item.product_id)) {
       // productId available in myItems array
    }
}, this);

Here is the sample code with open modal:

define([
    'uiComponent',
    'jquery',
    'underscore',
    'Magento_Ui/js/modal/modal'
], function (Component, $, _, modal) {
    'use strict';

    ...
    ...

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

        var myItems = ['1', '2', '3', '5'];
        _.each(items, function (item, key) {
            if(_.contains(myItems, item.product_id)) {
                $('body').append('<div id="modal">This is Model Content</div>');
                var options = {
                    type: 'popup',
                    responsive: true,
                    title: 'Main title',
                    buttons: [{
                        text: $.mage.__('Ok'),
                        class: '',
                        click: function () {
                            //After click code
                        }
                    }]
                };

                var popup = modal(options, $('#modal'));
                $('#modal').modal('openModal');
            }
        }, this);

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

        return items;
    },

    ...
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top