Html5 localStorage-based shopping cart JS script

Source: Internet
Author: User

I recently wrote a shopping cart JS script by referring to the Code on the Internet. It is very simple to directly write the code, shoppingCart. js: [javascript] utils = {setParam: function (name, value) {localStorage. setItem (name, value)}, getParam: function (name) {return localStorage. getItem (name) }}product = {id: 0, name: "", num: 0, price: 0.00, }; orderdetail = {username: "", phone: "", address: "", zipcode: "", totalNumber: 0, totalAmount: 0.00} cart = {// Add the product addproduct: function (pr Oduct) {var ShoppingCart = utils. getParam ("ShoppingCart"); if (ShoppingCart = null | ShoppingCart = "") {// Add product var jsonstr = {"productlist ": [{"id": product. id, "name": product. name, "num": product. num, "price": product. price}], "totalNumber": product. num, "totalAmount" :( product. price * product. num)}; utils. setParam ("ShoppingCart", "'" + JSON. stringify (jsonstr);} else {var jsonstr = JSON. parse (ShoppingCart. subst R (1, ShoppingCart. length); var productlist = jsonstr. productlist; var result = false; // you can check whether this item exists in the shopping cart. for (var I in productlist) {if (productlist [I]. id = product. id) {productlist [I]. num = parseInt (productlist [I]. num) + parseInt (product. num); result = true ;}} if (! Result) {// Add productlist directly without this product. push ({"id": product. id, "name": product. name, "num": product. num, "price": product. price});} // recalculates the total price of jsonstr. totalNumber = parseInt (jsonstr. totalNumber) + parseInt (product. num); jsonstr. totalAmount = parseFloat (jsonstr. totalAmount) + (parseInt (product. num) * parseFloat (product. price); orderdetail. totalNumber = jsonstr. totalNumber; orderdetail. totalAmount = jsonstr. totalAmount; // Save the shopping cart utils. setParam ("ShoppingCart", "'" + JSON. stringify (jsonstr) ;}}, // modify the number of purchased items updateproductnum: function (id, num) {var ShoppingCart = utils. getParam ("ShoppingCart"); var jsonstr = JSON. parse (ShoppingCart. substr (1, ShoppingCart. length); var productlist = jsonstr. productlist; for (var I in productlist) {if (productlist [I]. id = id) {jsonstr. totalNumber = parseInt (jsonstr. totalNumber) + (parseInt (num)-parseInt (productlist [I]. num); jsonstr. totalAmount = parseFloat (jsonstr. totalAmount) + (parseInt (num) * parseFloat (productlist [I]. price)-parseInt (productlist [I]. num) * parseFloat (productlist [I]. price); productlist [I]. num = parseInt (num); orderdetail. totalNumber = jsonstr. totalNumber; orderdetail. totalAmount = jsonstr. totalAmount; utils. setParam ("ShoppingCart", "'" + JSON. stringify (jsonstr); return ;}}, // get all the items in the shopping cart getproductlist: function () {var ShoppingCart = utils. getParam ("ShoppingCart"); var jsonstr = JSON. parse (ShoppingCart. substr (1, ShoppingCart. length); var productlist = jsonstr. productlist; orderdetail. totalNumber = jsonstr. totalNumber; orderdetail. totalAmount = jsonstr. totalAmount; return productlist;}, // determines whether there is existproduct: function (id) {var ShoppingCart = utils in the shopping cart. getParam ("ShoppingCart"); var jsonstr = JSON. parse (ShoppingCart. substr (1, ShoppingCart. length); var productlist = jsonstr. productlist; var result = false; for (var I in productlist) {if (productlist [I]. id = product. id) {result = true ;}} return result ;}, // Delete the eproduct: function (id) {var ShoppingCart = utils in the shopping cart. getParam ("ShoppingCart"); var jsonstr = JSON. parse (ShoppingCart. substr (1, ShoppingCart. length); var productlist = jsonstr. productlist; var list = []; for (var I in productlist) {if (productlist [I]. id = id) {jsonstr. totalNumber = parseInt (jsonstr. totalNumber)-parseInt (productlist [I]. num); jsonstr. totalAmount = parseFloat (jsonstr. totalAmount)-parseInt (productlist [I]. num) * parseFloat (productlist [I]. price);} else {list. push (productlist [I]) ;}} jsonstr. productlist = list; orderdetail. totalNumber = jsonstr. totalNumber; orderdetail. totalAmount = jsonstr. totalAmount; utils. setParam ("ShoppingCart", "'" + JSON. stringify (jsonstr) ;}}; It is also easy to use: [javascript] var product = {'id': id, // attribute names are enclosed in quotation marks, the attributes are separated by commas (,): 'name': 'hhh ', 'num': jq (' # text-4 '). val (), 'price': 199.9}; // Add the item to the cart of the shopping cart. addproduct (product); var productlist = cart. getproductlist (); // retrieve the cart item alert ('', 'item: '+ productlist [0]. id + ''+ productlist [0]. name + ''+ productlist [0]. num + ''+ productlist [0]. price, 'confirmed ');

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.