Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

addtocart.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. const CART_DATA = 'CART_DATA';
  2. let cartLength = 0;
  3. const CART_ADD = 'CART_ADD';
  4. function getCartData(){
  5. const data = localStorage.getItem(CART_DATA);
  6. if(data) return JSON.parse(data);
  7. return null;
  8. }
  9. function removeCartData(){
  10. localStorage.removeItem(CART_DATA);
  11. }
  12. function setCartData(data){
  13. localStorage.setItem(CART_DATA,JSON.stringify(data));
  14. }
  15. function setLengthCart(){
  16. const data = getCartData();
  17. if(!data) return;
  18. let length = 0;
  19. for(let item in data){
  20. let curr = data[item];
  21. length += parseInt(curr.quantity);
  22. }
  23. if(length>0){
  24. $('.cartnumcount').removeClass('d-none').html(length);
  25. }else{
  26. $('.cartnumcount').addClass('d-none').html(0);
  27. }
  28. }
  29. function getQuantityById(id){
  30. const data = getCartData();
  31. if(!data) return;
  32. for(let item in data){
  33. if(id == item){
  34. $('#quantity').val(products[item].quantity);
  35. }
  36. }
  37. }
  38. function updateCartQuantity(id,val){
  39. const data = getCartData();
  40. if(!data) return;
  41. data[id].quantity = val;
  42. setCartData(data);
  43. updateSummaryPriceAdd();
  44. }
  45. function removeCartItem(id){
  46. debugger;
  47. let data = getCartData();
  48. if(!data) return;
  49. let newdata = {};
  50. for(let item in data){
  51. if(item != id){
  52. newdata[item] = data[item];
  53. }
  54. }
  55. setCartData(newdata);
  56. updateSummaryPriceAdd();
  57. return Object.keys(newdata).length;
  58. }
  59. function updateSummaryPriceAdd(){
  60. let data = getCartData();
  61. let totalSummaryPrice = $('.totalSummaryPrice');
  62. let totalPrice = $('.totalPrice');
  63. let currecy = $('.currecy');
  64. let noofitems = $('.noofitems');
  65. if(!data) return;
  66. let len = 0;
  67. let totalAmt = 0;
  68. for(let item in data) {
  69. let {price,quantity} = data[item];
  70. totalAmt += parseInt(price*quantity);
  71. len += parseInt(quantity);
  72. }
  73. const [currencySymbol,amount] = getCurrencySymbol(totalAmt);
  74. noofitems.html(len);
  75. totalSummaryPrice.html(amount);
  76. totalPrice.html(amount);
  77. currecy.html(currencySymbol);
  78. }
  79. function initAddToCart(){
  80. debugger;
  81. addEventListeners();
  82. function addToCartFun(){
  83. const data = getCartData();
  84. let id = window.location.search.split('=')[1];
  85. const qty = parseInt($('#addtocart').data('qty'));
  86. if(!data){
  87. let cartObj = {};
  88. debugger;
  89. cartObj[$('#skudetailitem').val()] = {
  90. name:$('.productname').html(),
  91. price:$('.price').html(),
  92. quantity:$('#quantity').val(),
  93. img:$('.productDetailsMain img').attr('src'),
  94. sku:$('#skudetailitem').val(),
  95. totalQty:qty,
  96. productid:$('#productidtag').val(),
  97. itemid:$('#itemidtag').val()
  98. }
  99. addtoCart(cartObj);
  100. //$('.cartnumcount').removeClass('d-none').html(1);
  101. setLengthCart();
  102. }else{
  103. //if(!data[id]) data['length']++;
  104. let name = $('.productname').html();
  105. let price = $('.price').html();
  106. let quantity = $('#quantity').val();
  107. let img = $('.productDetailsMain img').attr('src');
  108. let sku = $('#skudetailitem').val();
  109. if(data[$('#skudetailitem').val()]){
  110. data[$('#skudetailitem').val()] = {
  111. name:name,
  112. price:price,
  113. quantity: parseInt(data[sku].quantity)+parseInt(quantity),
  114. img:img,
  115. sku:sku,
  116. totalQty:qty,
  117. productid:$('#productidtag').val(),
  118. itemid:$('#itemidtag').val()
  119. }
  120. addtoCart(data);
  121. }else{
  122. data[$('#skudetailitem').val()] = {
  123. name:name,
  124. price:price,
  125. quantity:parseInt(quantity),
  126. img:img,
  127. sku:sku,
  128. totalQty:qty,
  129. productid:$('#productidtag').val(),
  130. itemid:$('#itemidtag').val()
  131. }
  132. addtoCart(data);
  133. }
  134. }
  135. debugger;
  136. setLengthCart();
  137. }
  138. function addEventListeners(){
  139. $('#addtocart').html('Add to cart')
  140. $('#addtocart').off().click((e)=>{
  141. debugger
  142. $('.insufficientqty').addClass('d-none');
  143. const qty = parseInt($('#addtocart').data('qty'));
  144. debugger;
  145. if(parseInt($('#quantity').val())>qty){
  146. $('.insufficientqty').removeClass('d-none');
  147. return;
  148. }
  149. $('#addtocart').off().click(function (el){
  150. window.location.href = '/shopping-cart.html';
  151. });
  152. addToCartFun();
  153. debugger;
  154. toasterHelper('success',"Item added to cart");
  155. $('#addtocart').html('Go to cart');
  156. });
  157. $('.buynow').off('click').click(async (e)=>{
  158. addToCartFun();
  159. const res = await COOKIE_HELPER.validateToken();
  160. if(!res.response){
  161. localStorage.setItem(CART_ADD,true);
  162. window.location.href = '/login.html';
  163. $('.checkoutbtn').find('span').removeClass('d-none');
  164. $('.checkoutbtn').find('div').addClass('d-none');
  165. return;
  166. }
  167. window.location.href = '/selectdelivery.html';
  168. });
  169. }
  170. function addtoCart(data){
  171. const currData = getCartData();
  172. let newData = {...currData,...data};
  173. setCartData(newData);
  174. }
  175. }
  176. initAddToCart();