first commit
This commit is contained in:
Vendored
+248
@@ -0,0 +1,248 @@
|
||||
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').html(),
|
||||
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').html();
|
||||
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)=>{
|
||||
|
||||
$('.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');
|
||||
// debugger;
|
||||
// $('#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();
|
||||
Reference in New Issue
Block a user