Last active
August 24, 2016 08:47
-
-
Save jeka-kiselyov/8801531 to your computer and use it in GitHub Desktop.
JavaScript: Cart.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cart.js | |
// | |
// Copyright 2014 Jeka Kiselyov | |
// Released under the MIT license, http://opensource.org/licenses/MIT | |
// | |
// Usage: | |
// Cart.add(sku, name, 9.99, qty); | |
// Cart.remove(sku); | |
// if (Cart.has(sku)) { ... | |
// var price = Cart.priceTotal(); | |
// var priceHTML = Cart.priceTotal(true); // '0.00' | |
// for (var k in Cart.items) { console.log(Cart.items[k]; ... | |
window.Cart = { | |
items: {}, | |
itemsCount: 0, | |
itemsCountTotal: 0, | |
domain: false, | |
init: function() | |
{ | |
this.loadFromCookies(); | |
}, | |
clear: function() | |
{ | |
this.items = {}; | |
this.itemsCount = 0; | |
this.itemsCountTotal = 0; | |
this.saveToCookies(); | |
return true; | |
}, | |
add: function(id, name, price, qty) | |
{ | |
if (typeof(qty) !== 'undefined') | |
qty = parseInt(qty, 10); | |
else | |
qty = 1; | |
if (typeof(price) !== 'undefined') | |
price = parseFloat(price); | |
else | |
price = 0; | |
if (this.has(id)) | |
{ | |
this.items[id].qty+=qty; | |
} else { | |
this.items[id] = {id: id, name: name, qty: qty, price: price}; | |
this.itemsCount++; | |
} | |
this.itemsCountTotal+=qty; | |
this.saveToCookies(); | |
return true; | |
}, | |
remove: function(id) | |
{ | |
if (this.has(id)) | |
{ | |
this.itemsCount--; | |
this.itemsCountTotal-=this.items[id].qty; | |
delete this.items[id]; | |
this.saveToCookies(); | |
return true; | |
} else { | |
throw 'Invalid cart item identified'; | |
} | |
}, | |
increment: function(id, inc) | |
{ | |
if (typeof(inc) !== 'undefined') | |
inc = parseInt(inc, 10); | |
else | |
inc = 1; | |
if (this.has(id)) | |
{ | |
if (inc < 0 && -inc >= this.items[id].qty) /// don't allow to go in minus | |
{ | |
inc = -this.items[id].qty; | |
return this.remove(id); | |
} | |
this.itemsCountTotal+=inc; | |
this.items[id].qty+=inc; | |
this.saveToCookies(); | |
return true; | |
} else { | |
throw 'Invalid cart item identified'; | |
} | |
}, | |
has: function(id) | |
{ | |
if (typeof(this.items[id]) !== 'undefined' && typeof(this.items[id].qty) !== 'undefined' && this.items[id].qty > 0) | |
return true; | |
else | |
return false; | |
}, | |
getById: function(id) | |
{ | |
if (this.has(id)) | |
return this.items[id]; | |
else { | |
throw 'Invalid cart item identified'; | |
} | |
}, | |
count: function() | |
{ | |
return this.itemsCount; | |
}, | |
countTotal: function() | |
{ | |
return this.itemsCountTotal; | |
}, | |
priceTotal: function(format) | |
{ | |
var ret = 0; | |
for (var k in this.items) | |
if (typeof(this.items[k].qty) != 'undefined' && typeof(this.items[k].price) != 'undefined') | |
ret+=(this.items[k].price*this.items[k].qty); | |
if (format) | |
{ | |
ret = ret.toFixed(2); | |
} | |
return ret; | |
}, | |
saveToCookies: function() | |
{ | |
try | |
{ | |
this.docCookies.setItem('cart_items', JSON.stringify(this.items)); | |
return true; | |
} catch (e) { | |
} | |
}, | |
loadFromCookies: function() | |
{ | |
var items = {}; | |
try | |
{ | |
items = JSON.parse(this.docCookies.getItem('cart_items')); | |
} catch (e) { | |
} | |
for (var k in items) | |
if (typeof(items[k].id) !== 'undefined' && typeof(items[k].name) !== 'undefined' | |
&& typeof(items[k].qty) !== 'undefined' && typeof(items[k].price) !== 'undefined') | |
{ | |
this.add(items[k].id, items[k].name, items[k].price, items[k].qty); | |
} | |
return true; | |
}, | |
docCookies: { | |
getItem: function (sKey) { | |
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; | |
}, | |
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) { | |
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; } | |
var sExpires = "; expires=Fri, 31 Dec 9999 23:59:59 GMT"; | |
var sPath = "/"; | |
if (window.Cart.domain) | |
var sDomain = window.Cart.domain; | |
else | |
var sDomain = ''; | |
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : ""); | |
return true; | |
} | |
} | |
} | |
Cart.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment