Gravy
Gravy

Reputation: 12445

AngularJS Shopping Cart implementation - Add / Update items

I am trying to build a shopping cart service with AngularJS using ngCookies.

I can successfully add an item in the cart, however if the item already exists in the cart I want the addItem method to increment the quantity instead of pushing a new item to the items object.

How do I get the index of the item being added from the addItems method? Am I approaching this correctly or should I be setting the items index to the item.id to solve this problem?

app.factory('CartService', ['$cookieStore', function($cookieStore) {


var cart = {

    itemsCookie     : '',

    init            : function (itemsCookie) {
        this.itemsCookie = itemsCookie;
    },

    getAll          : function () {

        var items = $cookieStore.get(this.itemsCookie);

        return items;

    },

    addItem         : function(item, quantity) {

        // If cookie not defined, then put an empty array

        if (! ($cookieStore.get(this.itemsCookie) instanceof Array)) {

            $cookieStore.put(this.itemsCookie, []);

        }

        if (quantity === undefined) {
            quantity = 1;
        }

        var items = $cookieStore.get(this.itemsCookie);           

        items.push({
            id          :   item.id,
            quantity    :   quantity,
            price       :   item.price,
            name        :   item.name,
            thumb       :   item.thumb
        });

        $cookieStore.put(this.itemsCookie, items);

    },

    getItemByIndex  : function(index) {

        var items = $cookieStore.get(this.itemsCookie);

        return items[index];

    },

    updateQuantity  : function(index, quantity) {

        var items = $cookieStore.get(this.itemsCookie);

        items[index].quantity = quantity;

        $cookieStore.put(this.itemsCookie, items);

    },

Upvotes: 1

Views: 2076

Answers (1)

Gravy
Gravy

Reputation: 12445

I have solved it as follows: I am open to better suggestions -

    addItem         : function(item, quantity) {

        // If cookie not defined, then put an empty array

        if (! ($cookieStore.get(this.itemsCookie) instanceof Array)) {

            $cookieStore.put(this.itemsCookie, []);

        }

        if (quantity === undefined) {
            quantity = 1;
        }

        var items = $cookieStore.get(this.itemsCookie);

        var exists = -1;

        angular.forEach(items, function(obj, key) {

            if (obj.id == item.id)
            {
                exists = key;
            }
        });

        // Because array index 0 will be defined as false
        if (exists >= -1) {
            items[exists].quantity += quantity;
        }
        else {
            items.push({
                id          :   item.id,
                quantity    :   quantity,
                price       :   item.price,
                name        :   item.name,
                thumb       :   item.thumb
            });
        }

        $cookieStore.put(this.itemsCookie, items);

    },

Upvotes: 0

Related Questions