문제

Here is the function in minicart.js

getCartItems: function () {
            var items = this.getCartParam('items') || [];
            console.log(items);
            // want to get product id from items here
            items = items.slice(parseInt(-this.maxItemsToDisplay, 10));
            return items;
        }

Items are showing JSON array in console with all mini cart items data. But I am not able to get only product IDs from that.

Does anyone have any idea for the same?

Thanks!

도움이 되었습니까?

해결책

You can use undescore each function to get product id from items.

_.each(items, function (item, key) {
    console.log(item.product_id);
}, this);

So you final code looks like:

define([
    'uiComponent',
    'jquery',
    'underscore'
], function (Component, $, _) {

    ...
    ...

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

        _.each(items, function (item, key) {
            console.log(item.product_id);
        }, this);

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

        return items;
    },

    ...
    ...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top