var Toast = Swal.mixin({ toast: true, position: 'center', showConfirmButton: false, animation: true, timer: 5000, timerProgressBar: true, }); function updateCart() { // Perform an AJAX request to fetch the updated cart HTML fetch('path/to/your/cart/endpoint.php') .then(response => response.text()) .then(html => { // Update the cart section with the new HTML document.getElementById('shop_cart').innerHTML = html; // Optionally, you can also update the total and quantity counters here fetch('path/to/your/cart/summary/endpoint.php') .then(response => response.json()) .then(data => { document.getElementById('cart-total').innerHTML = data.subtotal.toFixed(2); document.getElementById('cart_qty_cls').innerHTML = data.totalQuantity; }); }) .catch(error => console.error('Error:', error)); } // Call updateCart() whenever you need to refresh the cart function objectToArray(cartObject) { return Object.values(cartObject); } function updatecarttotal(cart) { if (!Array.isArray(cart)) { console.log('cart is an object, converting to array:', cart); cart = objectToArray(cart); } let subtotal = 0; let totalQuantity = 0; cart.forEach(item => { subtotal += item.price * item.quantity; totalQuantity += item.quantity; }); document.getElementById('subtotal').innerHTML = subtotal.toFixed(2); document.getElementById('cart_qty_cls').innerHTML = totalQuantity; } function carttotalupdate(cart_up) { let subtotal = 0; let totalQuantity = 0; cart_up.forEach(pro => { subtotal += pro.price * pro.quantity; totalQuantity += pro.quantity; }); document.getElementById('subtotal_cart_item').innerHTML = subtotal.toFixed(2); document.getElementById('cart_qty_cls').innerHTML = totalQuantity; } function updatewishtotal(wish) { // Initialize subtotal and total quantity let subtotal = 0; let totalQuantity = 0; // Loop through each item in the cart to calculate the subtotal and total quantity wish.forEach(item => { subtotal += item.price * item.quantity; totalQuantity += item.quantity; }); document.getElementById('wish_qty_cls').innerHTML = totalQuantity; } //// Remove Cart Data/// function removefromcart(key){ $.ajax({ url: '/inc/common/ajax/addcart.php', type: 'POST', contentType: 'application/json', data: JSON.stringify({ action: 'remove', product_key: key }), success: function(response) { var data = JSON.parse(response); if (data.success===true) { $('#item-' + key).fadeOut('slow').slideUp('slow', function() {$(this).remove();}); $('#item_cart-' + key).fadeOut('slow').slideUp('slow', function() {$(this).remove();}); updatecarttotal(data.cart); // var currentSubtotal = parseFloat($('#subtotal').text()); // var newSubtotal = currentSubtotal - parseFloat(itemTotalPrice); // $('#subtotal').text(newSubtotal.toFixed(2)); $("#delete_to_cart_msg").slideDown(500).delay(1000).slideUp(600); } else if (data.success===false) { $("#message").text(data.error).slideDown(500).delay(1000).slideUp(600); } else{ $("#message").text("Something Went Wrong.").slideDown(500).delay(1000).slideUp(600); } } }); } function cart_remove(key){ $.ajax({ url: '/inc/common/ajax/addcart.php', type: 'POST', contentType: 'application/json', data: JSON.stringify({ action: 'remove', product_key: key }), success: function(response) { var data = JSON.parse(response); if (data.success===true) { $('#item_cart-' + key).fadeOut('slow').slideUp('slow', function() {$(this).remove();}); carttotalupdate(data.cart); $("#delete_to_cart_msg").slideDown(500).delay(1000).slideUp(600); } else if (data.success===false) { $("#message").text(data.error).slideDown(500).delay(1000).slideUp(600); } else{ $("#message").text("Something Went Wrong.").slideDown(500).delay(1000).slideUp(600); } } }); } ///////Add To Cart///// function addProductButton(cartdata){ var add_cart_data = JSON.parse(cartdata); var e=document.getElementById("pro_qty"); if(!e) { var qty = 1; } else{ var qty = e.value; } $.ajax({ url: '/inc/common/ajax/addcart.php', type: 'POST', contentType: 'application/json', data: JSON.stringify({ action: 'add', product: add_cart_data , quantity: qty }), success: function(response) { var data = JSON.parse(response); if (data.success===true) { $("#shop_cart").load(location.href + " #shop_cart"); updatecarttotal(data.cart); $("#add_to_cart_success_msg").slideDown(500).delay(1000).slideUp(600); setTimeout(function() { window.location.href = "/checkout"; }, 3000); } else if (data.success===false) { $("#message").text(data.error).slideDown(500).delay(1000).slideUp(600); } else { $("#message").text("Something Went Wrong.").slideDown(500).delay(1000).slideUp(600); } } }); } //////Add To Wishlist Function///// function toggleWishlist(element) { // Toggle the clicked class for the heart icon var heartIcon = element.querySelector('.heart_icon'); if (heartIcon && !heartIcon.classList.contains('clicked')) { // Add the clicked class if it is not already present heartIcon.classList.add('clicked'); } // Call the add_Wishlist function or your existing logic to handle the wishlist update var cartdata = element.closest('.lable-block').getAttribute('data-product-cart'); add_Wishlist(cartdata); } function add_Wishlist(cartdata){ var wish_data = JSON.parse(cartdata); $.ajax({ url: '/inc/common/ajax/add_wishlist_ajax.php', type: 'POST', contentType: 'application/json', data: JSON.stringify({action: 'add', wish_product: wish_data }), success: function(response) { var data = JSON.parse(response); if (data.success===true) { updatewishtotal(data.wishlist); $("#add_to_wishlist_success_msg").slideDown(500).delay(1000).slideUp(600); } else if (data.success===false) { $("#message").text(data.error).slideDown(500).delay(1000).slideUp(600); } else { $("#message").text("Something Went Wrong.").slideDown(500).delay(1000).slideUp(600); } } }); } function moveToCart(cartdata,wish_key){ var add_cart_data = JSON.parse(cartdata); var e=document.getElementById("product_quantity"); if(!e) { var qty = 1; } else{ var qty = e.value; } $.ajax({ url: '/inc/common/ajax/addcart.php', type: 'POST', contentType: 'application/json', data: JSON.stringify({ action: 'add', product: add_cart_data , quantity: qty }), success: function(response) { var data = JSON.parse(response); if (data.success===true) { $("#shop_cart").load(location.href + " #shop_cart"); updatecarttotal(data.cart); movefromwish(wish_key); $("#move_to_cart_success").slideDown(500).delay(1000).slideUp(600); } else if (data.success===false) { $("#message").text(data.error).slideDown(500).delay(1000).slideUp(600); } else { $("#message").text("Something Went Wrong.").slideDown(500).delay(1000).slideUp(600); } } }); } function removefromwish(key){ $.ajax({ url: '/inc/common/ajax/add_wishlist_ajax.php', type: 'POST', contentType: 'application/json', data: JSON.stringify({ action: 'remove', wish_key: key }), success: function(response) { var data = JSON.parse(response); if (data.success===true) { $('#item_wish-' + key).fadeOut('slow').slideUp('slow', function() {$(this).remove();}); updatewishtotal(data.wishlist); // var currentSubtotal = parseFloat($('#subtotal').text()); // var newSubtotal = currentSubtotal - parseFloat(itemTotalPrice); // $('#subtotal').text(newSubtotal.toFixed(2)); $("#delete_to_wish_msg").slideDown(500).delay(1000).slideUp(600); } else if (data.success===false) { $("#message").text(data.error).slideDown(500).delay(1000).slideUp(600); } else{ $("#message").text("Something Went Wrong.").slideDown(500).delay(1000).slideUp(600); } } }); } function movefromwish(wish_key){ $.ajax({ url: '/inc/common/ajax/add_wishlist_ajax.php', type: 'POST', contentType: 'application/json', data: JSON.stringify({ action: 'remove', wish_key: wish_key }), success: function(response) { var data = JSON.parse(response); if (data.success===true) { $('#item_wish-' + wish_key).fadeOut('slow').slideUp('slow', function() {$(this).remove();}); updatewishtotal(data.wishlist); } else if (data.success===false) { $("#message").text(data.error).slideDown(500).delay(1000).slideUp(600); } else{ $("#message").text("Something Went Wrong.").slideDown(500).delay(1000).slideUp(600); } } }); } function updateQuantity(action, key, currentQty) { var input = document.getElementById('pro_qty-' + key); var min = parseInt(input.getAttribute('data-min')); var max = parseInt(input.getAttribute('data-max')); var newQty = currentQty; if (action === 'increase' && currentQty < max) { newQty++; } else if (action === 'decrease' && currentQty > min) { newQty--; } else if(currentQty==max){ $('.qty_reach_msg_cart').css("display","block"); } if (newQty !== currentQty) { $.ajax({ url: '/inc/common/ajax/addcart.php', type: 'POST', contentType: 'application/json', data: JSON.stringify({ action: 'update', key: key, quantity: newQty }), success: function(response) { var data = JSON.parse(response); if (data.success) { $("#update_cart").load(location.href + " #update_cart"); $("#shop_cart").load(location.href + " #shop_cart"); carttotalupdate(data.cart); // $('.qty_success_msg_cart').css("display","block"); } else { $("#message").text("Something Went Wrong.").slideDown(500).delay(1000).slideUp(600); } } }); } } //// checkout page function ////// function displayForm(c) { if (c.value == "2") { jQuery('#new_bill_address_form').show(); } if (c.value == "1") { jQuery('#new_bill_address_form').hide(); } if (c.value == "3") { jQuery('#new_ship_address_form').show(); } if (c.value == "4") { jQuery('#new_ship_address_form').hide(); } } function displayForm2(c) { if (c.value == "3") { jQuery('#new_ship_address_form').show(); } if (c.value == "4") { jQuery('#new_ship_address_form').hide(); } } $('.register-btn').click(function(e){ e.preventDefault(); $("#billing_addr").hide(); $("#registration_form_bill").show(); const billingButton = document.getElementById('billingButton'); billingButton.setAttribute('data-bs-target', '#collapseTwo'); // Open the second step var collapseTwo = new bootstrap.Collapse(document.getElementById('collapseTwo'), { toggle: true }); // Optionally, hide the first step var collapseOne = new bootstrap.Collapse(document.getElementById('collapseOne'), { toggle: false }); }); ///// Login Form /////// $('#login_form').on('submit',function(e) { $.ajax({ url:'/inc/common/ajax/loginprocess.php', data:$(this).serialize(), type:'POST', success:function(data){ if(data=='success') { $("#login_form")[0].reset(); Toast.fire({ icon: 'success', title:'Login Successful', }); window.location.reload(true); } else if(data =='failed') { $("#login_form")[0].reset(); Toast.fire({ icon: 'error', title:' Incorrect Mobile or Password', }); } }, error:function(data){ $("#login_form")[0].reset(); Toast.fire({ icon: 'warning', title:'something went wrong', }); } }); e.preventDefault(); }); $('#login_form_checkout').on('submit',function(e) { $.ajax({ url:'/inc/common/ajax/loginprocess.php', data:$(this).serialize(), type:'POST', success:function(data){ if(data=='success') { $("#login_form_checkout")[0].reset(); Toast.fire({ icon: 'success', title:'Login Successful', }); window.location.reload(true); // const billingButton = document.getElementById('billingButton'); // billingButton.setAttribute('data-bs-target', '#collapseTwo'); // // Open the second step // var collapseTwo = new bootstrap.Collapse(document.getElementById('collapseTwo'), { // toggle: true // }); // // Optionally, hide the first step // var collapseOne = new bootstrap.Collapse(document.getElementById('collapseOne'), { // toggle: false // }); } else if(data =='failed') { $("#login_form_checkout")[0].reset(); Toast.fire({ icon: 'error', title:' Incorrect Mobile or Password', }); } }, error:function(data){ $("#login_form_checkout")[0].reset(); Toast.fire({ icon: 'warning', title:'something went wrong', }); } }); e.preventDefault(); }); $('#newsletter_form').on('submit',function(e) { $.ajax({ url:'/inc/common/ajax/newsletter.php', data:$(this).serialize(), type:'POST', success:function(data){ if(data=='success') { $("#newsletter_form")[0].reset(); Toast.fire({ icon: 'success', title:'Submit Successful', }); } else if(data =='failed') { $("#newsletter_form")[0].reset(); Toast.fire({ icon: 'error', title:'Something Went Wrong', }); } }, error:function(data){ $("#newsletter_form")[0].reset(); Toast.fire({ icon: 'warning', title:'something went wrong', }); } }); e.preventDefault(); }); ////Registration Function ////// function pincodeFinder(city){ $.ajax({ url:'/inc/common/ajax/pincodes_finder.php', data:{ city:city, }, type:'POST', beforeSend: function() { $('select[name="pincode"]').html(''); }, success:function(data){ var res = JSON.parse(data); if(res.stat == 1){ $('#pincodes_div').html(res.data); }else{ $('select[name="pincode"]').html(''); } } }); } function pincodeFinder2(city){ $.ajax({ url:'/inc/common/ajax/pincodes_finder.php', data:{ city:city, }, type:'POST', beforeSend: function() { $('select[name="pincode"]').html(''); }, success:function(data){ var res = JSON.parse(data); if(res.stat == 1){ $('#pincodes_div2').html(res.data); }else{ $('select[name="pincode"]').html(''); } } }); } function pincodeFinder3(city){ $.ajax({ url:'/inc/common/ajax/pincodes_finder.php', data:{ city:city, }, type:'POST', beforeSend: function() { $('select[name="pincode"]').html(''); }, success:function(data){ var res = JSON.parse(data); if(res.stat == 1){ $('#pincodes_div3').html(res.data); }else{ $('select[name="pincode"]').html(''); } } }); } ////order placed form ajax /// // $('#order_placed').on('submit',function(e) { // $.ajax({ // url:'/inc/common/ajax/checkout_process_ajax.php', // data:$(this).serialize(), // type:'POST', // dataType: 'json', // success:function(response){ // alert(response.order_id); // if(response.key == "rzp_test_tVm94Pc4TlS3Nm") // { // var options = { // "key": response.key, // Enter the Key ID generated from the Dashboard // "amount": response.amount, // Amount is in currency subunits // "currency": "INR", // "name": response.name, // "description": "Test Transaction", // "image": response.image, // "order_id": response.order_id, //This is a sample Order ID. // "prefill": { // "name": response.prefill.name, // Pre-filled name // "email": response.prefill.email, // Pre-filled email // "contact": response.prefill.contact // Pre-filled contact number // }, // "theme": { // "color": "#f8941c" // }, // "handler": function (response) { // console.log(response); // Log the response from Razorpay // // Further processing based on the response // } // }; // var rzp1 = new Razorpay(options); // rzp1.open(); // } // else if(response == "cash") // { // $("#order_placed")[0].reset(); // Toast.fire({ // icon: 'success', // title:'Order Confirm !', // }); // } // else if(response=="error") // { // $("#order_placed")[0].reset(); // Toast.fire({ // icon: 'danger', // title:'something went wrong', // }); // } // }, // error:function(data){ // $("#order_placed")[0].reset(); // Toast.fire({ // icon: 'error', // title:'Something went wrong', // }); // } // }); // e.preventDefault(); // }); $('#register_form').on('submit',function(e) { $.ajax({ url:'/inc/common/ajax/registerprocess.php', data:$(this).serialize(), type:'POST', success:function(data){ if(data > 0) { $("#register_form")[0].reset(); Toast.fire({ icon: 'success', title:'Registration Successful', }); } else if(data=='exist') { $("#register_form")[0].reset(); Toast.fire({ icon: 'warning', title:'Email Or Mobile already exist', }); } else if(data==2) { $("#register_form")[0].reset(); Toast.fire({ icon: 'error', title:'Referral Number not be match', }); } }, error:function(data){ $("#register_form")[0].reset(); Toast.fire({ icon: 'error', title:'Something went wrong', }); } }); e.preventDefault(); }); $('#register_form_checkout').on('submit',function(e) { $.ajax({ url:'/inc/common/ajax/registerprocess.php', data:$(this).serialize(), type:'POST', success:function(data){ if(data > 0) { $("#register_form_checkout")[0].reset(); Toast.fire({ icon: 'success', title:'Registration Successful', }); $.ajax({ type : 'GET', url : '/inc/common/ajax/sessAjax.php', data: { user : data, }, success : function(data){ window.location.reload(true); } }); } else if(data=='exist') { $("#register_form_checkout")[0].reset(); Toast.fire({ icon: 'warning', title:'Email Or Mobile already exist', }); } else if(data==2) { $("#register_form_checkout")[0].reset(); Toast.fire({ icon: 'error', title:'Referral Number not be match', }); } }, error:function(data){ $("#register_form_checkout")[0].reset(); Toast.fire({ icon: 'error', title:'Something went wrong', }); } }); e.preventDefault(); }); $(function() { var loc = new locationInfo(); loc.getStates(101); }); function get_billing_address(sessName){ var p = $('input[name=exist_billing]:checked').val(); if(p==1) { var billing_add = $('#billing_add :selected').val(); var user_id = sessName; if(user_id != "") { $.ajax({ type: "POST", url: "/inc/common/ajax/update_billing_address_AJAX.php", data: { sessName:user_id, billing_add:billing_add, }, success: function(data){ if($.trim(data) == 'success'){ Toast.fire({ icon: 'success', title:'Success!', }); const shipingButton = document.getElementById('shipingButton'); shipingButton.setAttribute('data-bs-target', '#collapseThree'); // Open the second step var collapseThree = new bootstrap.Collapse(document.getElementById('collapseThree'), { toggle: true }); // Optionally, hide the first step var collapseTwo = new bootstrap.Collapse(document.getElementById('collapseTwo'), { toggle: false }); }else if($.trim(data) == 'error'){ Toast.fire({ icon: 'error', title:'something went wrong!', }); } } }); } } if(p==2) { var firstname=$("#first_name_bill").val(); var lastname=$("#last_name_bill").val(); var mobiles=$("#mobile_bill").val(); var address_bill=$("#address_bill").val(); var city=$("#city_bill").val(); var postcode=$("#uname").val(); var state=$("#state_bill").val(); var sessName = sessName; $.ajax({ type: "POST", url: "/inc/common/ajax/billing_address_AJAX.php", data: { sessName : sessName, city : city, postcode : postcode, state : state, firstname: firstname, lastname : lastname, telephone: mobiles, address_1: address_bill, }, success: function(data){ if($.trim(data) == 'success'){ Toast.fire({ icon: 'success', title:'success!', }); const shipingButton = document.getElementById('shipingButton'); shipingButton.setAttribute('data-bs-target', '#collapseThree'); // Open the second step var collapseThree = new bootstrap.Collapse(document.getElementById('collapseThree'), { toggle: true }); // Optionally, hide the first step var collapseTwo = new bootstrap.Collapse(document.getElementById('collapseTwo'), { toggle: false }); }else if($.trim(data) == 'error'){ Toast.fire({ icon: 'error', title:'something went wrong!', }); } else if($.trim(data) == 'blank'){ Toast.fire({ icon: 'warning', title:'Please Fill Form!', }); } } }); } } function get_shipping_address(user_id){ var p = $('input[name=exist_shipping]:checked').val(); if(p==4) { var shipping_add = $('#shipping_add :selected').val(); var sessName = user_id; $.ajax({ type: "POST", url: "/inc/common/ajax/update_shipping_address_AJAX.php", data: { sessName:sessName, shipping_add:shipping_add, }, success: function(data){ var res = JSON.parse(data); if(res.stat == 1){ $('#final_delivered_shipping').val(res.shipping_total); $('#final_charge3').html(res.shipping_total); $('#final_charge4').html(res.shipping_total); $('#net_total_calculated').text(res.final_total); $('#net_total_calculated_summary').text(res.final_total); $('#final_amount_to_gateway').val(res.final_total); Toast.fire({ icon: 'success', title:'success!', }); const ConfirmButton = document.getElementById('ConfirmButton'); ConfirmButton.setAttribute('data-bs-target', '#collapseFour'); // Open the second step var collapseFour = new bootstrap.Collapse(document.getElementById('collapseFour'), { toggle: true }); // Optionally, hide the first step var collapseThree = new bootstrap.Collapse(document.getElementById('collapseThree'), { toggle: false }); } else{ Toast.fire({ icon: 'error', title:'something went wrong!', }); } } }); } if(p==3) { var firstname=$("#first_ship").val(); var lastname=$("#last_ship").val(); var telephone=$("#mobile_ship").val(); var address_1=$("#address_shipi").val(); var city=$("#city_ship").val(); var postcode=$("#uname").val(); var state=$("#state_ship option:selected").val(); var sessName = user_id; $.ajax({ type: "POST", url : "/inc/common/ajax/shipping_address_AJAX.php", data: { sessName : sessName, lastname : lastname, city : city, postcode : postcode, state : state, firstname : firstname, telephone : telephone, address_1 : address_1, }, success: function(data){ var res = JSON.parse(data); if(res.stat == 1){ $('#final_delivered_shipping').val(res.shipping_total); $('#final_charge3').html(res.shipping_total); $('#final_charge4').html(res.shipping_total); $('#net_total_calculated').text(res.final_total); $('#net_total_calculated_summary').text(res.final_total); $('#final_amount_to_gateway').val(res.final_total); Toast.fire({ icon: 'success', title:'success!', }); const ConfirmButton = document.getElementById('ConfirmButton'); ConfirmButton.setAttribute('data-bs-target', '#collapseFour'); // Open the second step var collapseFour = new bootstrap.Collapse(document.getElementById('collapseFour'), { toggle: true }); // Optionally, hide the first step var collapseThree = new bootstrap.Collapse(document.getElementById('collapseThree'), { toggle: false }); } else if(res.message == 'blank'){ Toast.fire({ icon: 'warning', title:'Please Fill Form!', }); } else{ Toast.fire({ icon: 'error', title:'something went wrong!', }); } } }); } } //////Coupon Module javascript///////// $('#coupon_form').on('submit',function(e) { var coupon_discount =0; var coupon_name; $.ajax({ url:'/inc/common/ajax/coupon_apply_ajax.php', data:$(this).serialize(), type:'POST', success:function(data){ console.log(data); var json = $.parseJSON(data); if(json.status ==0) { $('#invalid').show().html('