Shopping Cart JS script based on HTML5 Localstorage

Source: Internet
Author: User

http://blog.csdn.net/wangqiuyun/article/details/8435649

Recently in doing HTML5 this piece, refer to the online code to write a shopping cart JS script, very simple, directly on the code, Shoppingcart.js:

[JavaScript]View PlainCopy
  1. Utils = {
  2. SetParam: function (name,value) {
  3. Localstorage.setitem (Name,value)
  4. },
  5. GetParam: function (name) {
  6. return Localstorage.getitem (name)
  7. }
  8. }
  9. product={
  10. id:0,
  11. Name: "",
  12. num:0,
  13. price:0.00,
  14. };
  15. orderdetail={
  16. Username: "",
  17. Phone: "",
  18. Address: "",
  19. ZipCode: "",
  20. totalnumber:0,
  21. totalamount:0.00
  22. }
  23. Cart = {
  24. //Add items to your shopping cart
  25. Addproduct:function (product) {
  26. var ShoppingCart = Utils.getparam ("ShoppingCart");
  27. if (shoppingcart==null| | shoppingcart=="") {
  28. //First time to add products
  29. var jsonstr = {"productlist": [{"id":p roduct.id,"name":p roduct.name,"num":p roduct.num,"  Price ":p Roduct.price}]," Totalnumber ":p roduct.num," TotalAmount ":(Product.price*product.num)};
  30. Utils.setparam ("ShoppingCart","'" +json.stringify (JSONSTR));
  31. }else{
  32. var jsonstr = Json.parse (Shoppingcart.substr (1,shoppingcart.length));
  33. var productlist = jsonstr.productlist;
  34. var result=false;
  35. //Find out if the item is in the cart
  36. For (var i in productlist) {
  37. if (productlist[i].id==product.id) {
  38. Productlist[i].num=parseint (Productlist[i].num) +parseint (product.num);
  39. result = true;
  40. }
  41. }
  42. if (!result) {
  43. //Without the product, add it directly
  44. Productlist.push ({"id":p roduct.id,"name":p roduct.name,"num":p roduct.num,"Price":p Roduct.price}  );
  45. }
  46. //Recalculate Total Price
  47. Jsonstr.totalnumber=parseint (Jsonstr.totalnumber) +parseint (product.num);
  48. Jsonstr.totalamount=parsefloat (Jsonstr.totalamount) + (parseint (product.num) *parsefloat (Product.price));
  49. Orderdetail.totalnumber = Jsonstr.totalnumber;
  50. Orderdetail.totalamount = Jsonstr.totalamount;
  51. //Save Shopping Cart
  52. Utils.setparam ("ShoppingCart","'" +json.stringify (JSONSTR));
  53. }
  54. },
  55. //Modify the quantity of goods to buy
  56. Updateproductnum:function (id,num) {
  57. var ShoppingCart = Utils.getparam ("ShoppingCart");
  58. var jsonstr = Json.parse (Shoppingcart.substr (1,shoppingcart.length));
  59. var productlist = jsonstr.productlist;
  60. For (var i in productlist) {
  61. if (productlist[i].id==id) {
  62. Jsonstr.totalnumber=parseint (Jsonstr.totalnumber) + (parseint (num)-parseint (productlist[i].num));
  63. Jsonstr.totalamount=parsefloat (Jsonstr.totalamount) + ((parseint (num) *parsefloat (productlist[i].price))-parseInt (productlist[i].num) *parsefloat (Productlist[i].price));
  64. Productlist[i].num=parseint (num);
  65. Orderdetail.totalnumber = Jsonstr.totalnumber;
  66. Orderdetail.totalamount = Jsonstr.totalamount;
  67. Utils.setparam ("ShoppingCart","'" +json.stringify (JSONSTR));
  68. return;
  69. }
  70. }
  71. },
  72. //Get all items in the shopping cart
  73. Getproductlist:function () {
  74. var ShoppingCart = Utils.getparam ("ShoppingCart");
  75. var jsonstr = Json.parse (Shoppingcart.substr (1,shoppingcart.length));
  76. var productlist = jsonstr.productlist;
  77. Orderdetail.totalnumber = Jsonstr.totalnumber;
  78. Orderdetail.totalamount = Jsonstr.totalamount;
  79. return productlist;
  80. },
  81. //Determine if a product exists in the shopping cart
  82. Existproduct:function (ID) {
  83. var ShoppingCart = Utils.getparam ("ShoppingCart");
  84. var jsonstr = Json.parse (Shoppingcart.substr (1,shoppingcart.length));
  85. var productlist = jsonstr.productlist;
  86. var result=false;
  87. For (var i in productlist) {
  88. if (productlist[i].id==product.id) {
  89. result = true;
  90. }
  91. }
  92. return result;
  93. },
  94. //delete items in cart
  95. Deleteproduct:function (ID) {
  96. var ShoppingCart = Utils.getparam ("ShoppingCart");
  97. var jsonstr = Json.parse (Shoppingcart.substr (1,shoppingcart.length));
  98. var productlist = jsonstr.productlist;
  99. var list=[];
  100. For (var i in productlist) {
  101. if (productlist[i].id==id) {
  102. Jsonstr.totalnumber=parseint (Jsonstr.totalnumber)-parseint (productlist[i].num);
  103. Jsonstr.totalamount=parsefloat (Jsonstr.totalamount)-parseint (productlist[i].num) *parseFloat (Productlist[i]. Price);
  104. }else{
  105. List.push (Productlist[i]);
  106. }
  107. }
  108. Jsonstr.productlist = list;
  109. Orderdetail.totalnumber = Jsonstr.totalnumber;
  110. Orderdetail.totalamount = Jsonstr.totalamount;
  111. Utils.setparam ("ShoppingCart","'" +json.stringify (JSONSTR));
  112. }
  113. };

It's also easy to use:

[JavaScript]View PlainCopy
    1. var product =
    2. {
    3. ' ID ': ID, //attribute name enclosed in quotation marks, separated by commas between attributes
    4. ' name ': ' hhh ',
    5. ' num ': JQ (' #text-4 '). Val (),
    6. ' price ': 199.9
    7. };
    8. Add items to your shopping cart
    9. Cart.addproduct (product);
    10. var productlist=cart.getproductlist (); //Remove shopping cart items
    11. Alert (', ' product: ' +productlist[0].id+ '+productlist[0].name+ '+productlist[0].num+ '+  Productlist[0].price, ' OK ');

Shopping Cart JS script based on HTML5 Localstorage

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.