123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- const CART_DATA = 'CART_DATA';
-
- let cartLength = 0;
- const CART_ADD = 'CART_ADD';
- function getCartData(){
- const data = localStorage.getItem(CART_DATA);
- if(data) return JSON.parse(data);
- return null;
- }
-
- function removeCartData(){
- localStorage.removeItem(CART_DATA);
- }
-
- function setCartData(data){
- localStorage.setItem(CART_DATA,JSON.stringify(data));
- }
-
- function setLengthCart(){
- const data = getCartData();
- if(!data) return;
-
-
- let length = 0;
-
- for(let item in data){
- let curr = data[item];
- length += parseInt(curr.quantity);
- }
-
- if(length>0){
- $('.cartnumcount').removeClass('d-none').html(length);
- }else{
- $('.cartnumcount').addClass('d-none').html(0);
- }
-
- }
-
-
- function getQuantityById(id){
- const data = getCartData();
- if(!data) return;
-
- for(let item in data){
- if(id == item){
- $('#quantity').val(products[item].quantity);
- }
- }
- }
-
- function updateCartQuantity(id,val){
- const data = getCartData();
- if(!data) return;
-
-
- data[id].quantity = val;
-
-
- setCartData(data);
- updateSummaryPriceAdd();
-
- }
-
- function removeCartItem(id){
-
- let data = getCartData();
- if(!data) return;
-
- let newdata = {};
-
- for(let item in data){
- if(item != id){
- newdata[item] = data[item];
- }
- }
-
- setCartData(newdata);
- updateSummaryPriceAdd();
-
- return Object.keys(newdata).length;
- }
-
- function updateSummaryPriceAdd(){
- let data = getCartData();
- let totalSummaryPrice = $('.totalSummaryPrice');
- let totalPrice = $('.totalPrice');
- let currecy = $('.currecy');
- let noofitems = $('.noofitems');
- if(!data) return;
- let len = 0;
- let totalAmt = 0;
- for(let item in data) {
- let {price,quantity} = data[item];
- totalAmt += parseInt(price*quantity);
- len += parseInt(quantity);
- }
- const [currencySymbol,amount] = getCurrencySymbol(totalAmt);
- noofitems.html(len);
-
- totalSummaryPrice.html(amount);
- totalPrice.html(amount);
- currecy.html(currencySymbol);
-
-
- }
-
- function initAddToCart(){
-
- addEventListeners();
-
- function addToCartFun(){
- const data = getCartData();
-
- let id = window.location.search.split('=')[1];
- const qty = parseInt($('#addtocart').data('qty'));
- if(!data){
-
- let cartObj = {};
-
-
- cartObj[$('#skudetailitem').val()] = {
- name:$('.productname').html(),
- price:$('.price').data('price'),
- quantity:$('#quantity').val(),
- img:$('.productDetailsMain img').attr('src'),
- sku:$('#skudetailitem').val(),
- totalQty:qty,
- productid:$('#productidtag').val(),
- itemid:$('#itemidtag').val()
- }
-
-
- addtoCart(cartObj);
- //$('.cartnumcount').removeClass('d-none').html(1);
- setLengthCart();
-
- }else{
- //if(!data[id]) data['length']++;
- let name = $('.productname').html();
- let price = $('.price').data('price');
- let quantity = $('#quantity').val();
- let img = $('.productDetailsMain img').attr('src');
- let sku = $('#skudetailitem').val();
-
-
-
- if(data[$('#skudetailitem').val()]){
- data[$('#skudetailitem').val()] = {
- name:name,
- price:price,
- quantity: parseInt(data[sku].quantity)+parseInt(quantity),
- img:img,
- sku:sku,
- totalQty:qty,
- productid:$('#productidtag').val(),
- itemid:$('#itemidtag').val()
- }
- addtoCart(data);
- }else{
- data[$('#skudetailitem').val()] = {
- name:name,
- price:price,
- quantity:parseInt(quantity),
- img:img,
- sku:sku,
- totalQty:qty,
- productid:$('#productidtag').val(),
- itemid:$('#itemidtag').val()
- }
- addtoCart(data);
- }
-
- }
-
- setLengthCart();
-
- }
-
- function addEventListeners(){
- $('#addtocart').html('Add to cart');
- $('#addtocart').removeAttr('gotocart');
- $('#addtocart').off('click').click((e)=>{
- debugger
- $('.insufficientqty').addClass('d-none');
- const qty = parseInt($('#addtocart').data('qty'));
-
- if(parseInt($('#quantity').val())>qty){
- $('.insufficientqty').removeClass('d-none');
- return;
- }
- $('#addtocart').off().click(function (el){
- window.location.href = '/shopping-cart.html';
-
- });
-
- addToCartFun();
-
- toasterHelper('success',"Item added to cart","toast-top-right");
-
- $('#addtocart').html('Go to cart');
-
- $('#addtocart').attr('gotocart',1);
- });
-
- // $('.quantityHTML #quantity').off('change').change(function (e){
- // let sku = $('#skudetailitem').val();
- // let data = getCartData();
- // const qty = parseInt($('#addtocart').data('qty'));
- // let isCart = $('#addtocart').attr('gotocart');
- // if(!isCart) return;
- // if(parseInt($('#quantity').val())>qty){
- // $('.insufficientqty').removeClass('d-none');
- // return;
- // }
- // if(!data[sku]){
- // return;
- // }
-
- // data[sku].quantity = $('.quantityHTML #quantity').val();
- // toasterHelper("success","Your item quantity was been updated","toast-top-right")
- // setCartData(data);
- // setLengthCart();
- // })
-
- $('.buynow').off('click').click(async (e)=>{
- addToCartFun();
- const res = await COOKIE_HELPER.validateToken();
- if(!res.response){
- localStorage.setItem(CART_ADD,true);
- window.location.href = '/login.html';
-
- $('.checkoutbtn').find('span').removeClass('d-none');
- $('.checkoutbtn').find('div').addClass('d-none');
- return;
- }
- window.location.href = '/selectdelivery.html';
-
- });
- }
-
- function addtoCart(data){
- const currData = getCartData();
- let newData = {...currData,...data};
- setCartData(newData);
- }
- }
-
- initAddToCart();
|