jQuery(document).ready(function ($) { // console.log("Custom Product Actions Loaded - Version 1.0.5"); // Add to Cart functionality $(document).on('click', '.add-to-cart-btn', function (e) { e.preventDefault(); e.stopPropagation(); var $button = $(this); // Prevent duplicate clicks while processing if ($button.prop('disabled') || $button.hasClass('processing')) { return false; } var productId = $button.data('product-id'); var nonce = $button.data('nonce'); // Disable button and show loading $button.prop('disabled', true).addClass('processing').text('Adding...'); $.ajax({ url: wc_add_to_cart_params.ajax_url, type: 'POST', data: { action: 'custom_add_to_cart', product_id: productId, quantity: 1, nonce: nonce }, success: function (response) { if (response.success) { // Update button $button.text('Added!').removeClass('processing').addClass('added'); // Show success message showNotification('Product added to cart successfully!', 'success'); // Update cart fragments (WooCommerce standard way) if (response.data.fragments) { $.each(response.data.fragments, function (key, value) { $(key).replaceWith(value); }); } // Update cart count if you have a cart icon if (response.data.cart_count) { $('.cart-count').text(response.data.cart_count); // Also update WooCommerce default cart count elements $('.cart-contents-count, .count').text(response.data.cart_count); } // Trigger WooCommerce cart update events $(document.body).trigger('added_to_cart', [response.data.fragments, response.data.cart_hash, $button]); $(document.body).trigger('wc_fragment_refresh'); // Reset button after 2 seconds setTimeout(function () { $button.prop('disabled', false).text('Add to Cart').removeClass('added'); }, 2000); } else { showNotification(response.data.message || 'Failed to add product', 'error'); $button.prop('disabled', false).removeClass('processing').text('Add to Cart'); } }, error: function () { showNotification('Something went wrong. Please try again.', 'error'); $button.prop('disabled', false).removeClass('processing').text('Add to Cart'); } }); return false; // Prevent any default action or form submission }); // Enquiry button functionality (existing) $(document).on('click', '.enquire-btn', function (e) { e.preventDefault(); var productId = $(this).data('product-id'); var productName = $(this).data('product-name'); // Open enquiry modal/form openEnquiryForm(productId, productName); }); // Notification helper function function showNotification(message, type) { // Remove existing notifications $('.custom-notification').remove(); // Create notification element var $notification = $('