No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

addtocart.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. addEventListeners();
  81. function addToCartFun(){
  82. const data = getCartData();
  83. let id = window.location.search.split('=')[1];
  84. const qty = parseInt($('#addtocart').data('qty'));
  85. if(!data){
  86. let cartObj = {};
  87. debugger;
  88. cartObj[$('#skudetailitem').val()] = {
  89. name:$('.productname').html(),
  90. price:$('.price').html(),
  91. quantity:$('#quantity').val(),
  92. img:$('.productDetailsMain img').attr('src'),
  93. sku:$('#skudetailitem').val(),
  94. totalQty:qty
  95. }
  96. addtoCart(cartObj);
  97. //$('.cartnumcount').removeClass('d-none').html(1);
  98. setLengthCart();
  99. }else{
  100. //if(!data[id]) data['length']++;
  101. let name = $('.productname').html();
  102. let price = $('.price').html();
  103. let quantity = $('#quantity').val();
  104. let img = $('.productDetailsMain img').attr('src');
  105. let sku = $('#skudetailitem').val();
  106. if(data[$('#skudetailitem').val()]){
  107. data[$('#skudetailitem').val()] = {
  108. name:name,
  109. price:price,
  110. quantity: parseInt(data[sku].quantity)+parseInt(quantity),
  111. img:img,
  112. sku:sku,
  113. totalQty:qty
  114. }
  115. addtoCart(data);
  116. }else{
  117. data[$('#skudetailitem').val()] = {
  118. name:name,
  119. price:price,
  120. quantity:parseInt(quantity),
  121. img:img,
  122. sku:sku,
  123. totalQty:qty
  124. }
  125. addtoCart(data);
  126. }
  127. }
  128. setLengthCart();
  129. }
  130. function addEventListeners(){
  131. $('#addtocart').html('Add to cart')
  132. $('#addtocart').off().click((e)=>{
  133. debugger
  134. $('.insufficientqty').addClass('d-none');
  135. const qty = parseInt($('#addtocart').data('qty'));
  136. debugger;
  137. if(parseInt($('#quantity').val())>qty){
  138. $('.insufficientqty').removeClass('d-none');
  139. return;
  140. }
  141. $('#addtocart').off().click(function (el){
  142. window.location.href = '/shopping-cart.html';
  143. });
  144. addToCartFun();
  145. debugger;
  146. toasterHelper('success',"Item added to cart");
  147. $('#addtocart').html('Go to cart');
  148. });
  149. $('.buynow').off('click').click(async (e)=>{
  150. addToCartFun();
  151. const res = await COOKIE_HELPER.validateToken();
  152. if(!res.response){
  153. localStorage.setItem(CART_ADD,true);
  154. window.location.href = '/login.html';
  155. $('.checkoutbtn').find('span').removeClass('d-none');
  156. $('.checkoutbtn').find('div').addClass('d-none');
  157. return;
  158. }
  159. window.location.href = '/selectdelivery.html';
  160. });
  161. }
  162. function addtoCart(data){
  163. const currData = getCartData();
  164. let newData = {...currData,...data};
  165. setCartData(newData);
  166. }
  167. }
  168. initAddToCart();