Addtocart js
🧩 Syntax:
jQuery(document).ready(function($) {
// Handle clicking on the plus button
$(document).on('click', '.plus', function(e) {
e.preventDefault();
var quantityInput = $(this).siblings('input[name="quantity"]');
var currentValue = parseInt(quantityInput.val());
quantityInput.val(currentValue + 1);
});
// Handle clicking on the minus button
$(document).on('click', '.minus', function(e) {
e.preventDefault();
var quantityInput = $(this).siblings('input[name="quantity"]');
var currentValue = parseInt(quantityInput.val());
if (currentValue > 1) {
quantityInput.val(currentValue - 1);
}
});
});
jQuery(document).ready(function($) {
// $(document).on('click', '.elementor-menu-cart__container', function(e) {
// $('.elementor-widget-woocommerce-menu-cart').removeClass('elementor-menu-cart--shown');
// });
// $(document).on('click', '.elementor-menu-cart__close-button', function(e) {
// $('.elementor-widget-woocommerce-menu-cart').removeClass('elementor-menu-cart--shown');
// });
// Handle clicking on the add to cart button
$(document).on('click', '#btnShop', function(e) {
e.preventDefault();
var product_id = $(this).data('product-id');
var quantity = 1; // Default quantity
// If quantity input exists, get its value
var quantityInput = $(this).siblings('#quantity-ui').find('input[name="quantity"]');
if (quantityInput.length) {
quantity = parseInt(quantityInput.val());
}
// setTimeout(function() {
// let qty = $('.elementor-button-icon-qty').text();
// // alert(qty);
// if(qty == '1'){
// $('.elementor-widget-woocommerce-menu-cart').addClass('elementor-menu-cart--shown');
// // $('.elementor-widget-woocommerce-menu-cart').show();
// }
// }, 1300);
console.log('quantity1:', quantity)
$(this).addClass('added');
// AJAX call to add the product to the cart
$.ajax({
type: 'POST',
url: ajax_object.ajaxurl,
// Use WordPress provided AJAX URL
data: {
'action': 'add_to_cart', // Action name defined in PHP function
'product_id': product_id,
'quantity': quantity
},
success: function(response) {
// alert('success');
console.log('quantity:', quantity)
// Handle success response
console.log('response', response);
// Optionally, you can redirect to the cart page or update the cart icon
// window.location.href = 'URL_OF_CART_PAGE';
},
error: function(xhr, ajaxOptions, thrownError) {
// Handle error
console.log('Error:::::', xhr.responseText);
}
});
$.ajax({
type: 'POST',
url: window.location.origin + '/?wc-ajax=add_to_cart',
// url: 'http://localhost:10241/?wc-ajax=add_to_cart', // Use the specific action URL
data: {
'product_id': product_id,
'quantity': quantity
},
success: function(response) {
console.log('response', response.fragments);
var newPrice = $(response.fragments['.cart-contents span.amount']).text();
// Update the price span
$('.woocommerce-Price-amount.amount').text(newPrice);
var numberOfItems = $(response.fragments['.cart-contents span.number-of-items']).text();
setTimeout(function() {
console.log('NUMBER OF ITEMS', numberOfItems);
$('.elementor-button-icon-qty').text(numberOfItems);
}, 300);
var newProductHTML = response.fragments['div.widget_shopping_cart_content'];
// $('.elementor-menu-cart__main').append($(newProductHTML));
console.log('html', newProductHTML);
// $('.elementor-menu-cart__main').append(newProductHTML);
if ($('.widget_shopping_cart_content').length && !$('.widget_shopping_cart_content').is(':empty')) {
$('.widget_shopping_cart_content').empty();
}
setTimeout(function() {
$('.widget_shopping_cart_content').append($(newProductHTML));
// $('.elementor-menu-cart__products').append(newProductHTML);
}, 500);
// alert('Product added to cart.');
console.log('Response:', response);
// Handle success response
},
error: function(xhr, ajaxOptions, thrownError) {
// alert('Failed to add product to cart.');
// Handle error
console.log('Error:', xhr.responseText);
}
});
// $.ajax({
// type: 'POST',
// url: 'http://localhost:10241/?wc-ajax=get_refreshed_fragments', // Use the specific action URL
// data: {
// 'product_id': product_id,
// 'quantity': quantity
// },
// success: function(response) {
// // alert('Product added to cart.');
// console.log('Response:', response);
// // Parse the response and get the number of items in the cart
// // var numberOfItems = $(response.fragments['.cart-contents span.number-of-items']).text();
// // setTimeout(function() {
// // console.log('NUMBER OF ITEMS', numberOfItems);
// // $('.elementor-button-icon-qty').text(numberOfItems);
// // }, 300);
// // Update the .elementor-button-icon-qty with the new number of items
// },
// error: function(xhr, ajaxOptions, thrownError) {
// alert('Failed to add product to cart.');
// // Handle error
// console.log('Error:', xhr.responseText);
// }
// });
});
});