snapjay / ngCart

Really simple shopping cart for AngularJS
http://ngcart.snapjay.com
380 stars 236 forks source link

How to add multiple Items of the same core product ID #83

Open wizyoua opened 7 years ago

wizyoua commented 7 years ago

So in my application we are using ng-cart but I'll make a simple example to show how you can make the application more powerful:

Say you have 3 Items: Iphone1 Iphone2 Iphone3

ng-cart has the "add to cart" button which will add say Iphone 1 to the cart

the id:of Iphone 1 lets say is id="1"

But what if the application has more options to choose like color, size, carrier. Well after passing that object into the data="{...}" attribute, we need to be able to add a unique Iphone1 to the cart so that every time you click add to cart a unique item is added to the checkout screen.

All you have to do is go into ngCart.js file - Line 213

this line grabs the initial id before even doing anything else in the program

 item.prototype.setId = function(id){
    if (id)  this._id = id;
        else {
            $log.error('An ID must be provided');
        }
    }

In my case I wanted to simply append some number to differentiate every item that I click "add to cart" with so I did:

item.prototype.setId = function(id){
        //create Unique Id here
        //get millisecond var
        var timestamp = new Date().getUTCMilliseconds();
        //append mill var with id var to create unique id
        if (id)  this._id = id + timestamp;
        else {
            $log.error('An ID must be provided');
        }
    }; 

Now you can go and add a item to the cart, click checkout and check console. You will see the id of that item be appended with 3 more numbers.

Current Thoughts on this:

Voronov commented 7 years ago

if you have iphone 1 with options e. g. size of memory 32, 64, 128 or with colors green, red, blue. You can make id for ngCart by adding them to id, Example: If you have id="1" for your iphone id can be id+size+color and it will be 132red. It will be unique and repeatable if user will try to add again.