cornershop
Shopping cart that saves to LocalStorage.
installation
npm/browserify:
$ npm install cornershop
component:
$ component install binocarlos/cornershop
usage
cart access
Each cart has a name - this is used for the localstorage variable name and so you can have as many carts as you want.
var shop = ;var cart = ;
load()
To load the cart from localstorage either pass true as the second argument:
var shop = ;var cart = ;
or call the .load method manually:
var shop = ;var cart = ;cart;
save()
To save the cart to localstorage call the .save() method:
var shop = ;var cart = ;cart; // do stuff cart;
items
The cart works on a list of items. An item is a plain JavaScript object with these properties:
- id (10)
- name (Superman Poster)
- price (12.5)
- qty (2)
You can add any meta-data to an item that you want, for example a description and image:
- desc (10x5 - superman logo bottom-right)
- image (/img/shop/superman.png)
Access the items in the cart:
// an array of itemsvar items = cartitems; var item = items0;console; /*{ id:10, name:'Superman Poster', desc:'10x5 - superman logo bottom-right', price:12.5, qty:2, image:'/img/shop/superman.png'}*/
addItem({...})
To add a new item to the cart:
cart
getItem(id)
To edit an item you fetch it by id and change properties of the item object and then save the cart.
var item = cart;itemqty++;cart;
removeItem(id)
Remove an item with an id - this auto-saves:
cart;
getTotal(withExtras)
The cart will return the current total - this is based on item.price * item.qty:
var total = cart;
var totalwithshipping = cart;
extras
This accounts for extra things like shipping and any other things that are not products but have a charge associated.
setExtra(field, {...})
Add an extra item to the cart - like shipping.
Each extra item should have a 'name', 'price' and 'qty' field like the products.
cart
removeExtra(field)
Remove an extra field
cart;
getExtras()
Return an array of the extra settings in the cart:
var extra_array = cart;
getExtraTotal()
Return the sub-total just for the extra items
License
MIT