File: /var/www/kevin-demo/wp-content/themes/fitmencook/js/custom-add-to-cart.js
$(document).ready(function($) {
console.log('Document is ready');
// Uklonite prethodne click event hendler-e pre nego što dodate novi
$('.ingredient-add-to-cart').off('click');
$('.ingredient-add-to-cart').on('click', function(event) {
event.preventDefault();
var productId = $(this).data('product-id');
var quantity = 1; // Ovdje smo hard kodirali kvantitet na 1
console.log('Product ID:', productId); // Provera Product ID-a
console.log('Quantity:', quantity); // Provera Quantity
var $this = $(this); // Zadržite referencu na trenutni element
// Flag to prevent multiple submissions
if ($this.data('clicked')) {
return;
}
$this.data('clicked', true);
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.ajax_url,
data: {
action: 'woocommerce_add_to_cart',
product_id: productId,
quantity: quantity,
},
cache: false,
success: function(response) {
console.log('Response:', response); // Provera odgovora
if (response.error && response.product_url) {
window.location = response.product_url;
} else {
// Promenite tekst dugmeta na "Added to Cart"
$this.html('<span>Added to Cart</span>');
// Dodajte klasu za prikazivanje "Added to Cart"
$this.removeClass('ingredient-add-to-cart').addClass('ingredient-add-to-cart-view-cart');
// Alternativno, možete promeniti URL dugmeta na korpu
$this.attr('href', wc_add_to_cart_params.cart_url);
}
},
});
});
});