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 = $('
' + message + '
'); // Add to body $('body').append($notification); // Show with animation setTimeout(function () { $notification.addClass('show'); }, 10); // Auto-hide after 3 seconds setTimeout(function () { $notification.removeClass('show'); setTimeout(function () { $notification.remove(); }, 300); }, 3000); } // Open enquiry form function (modify based on your existing modal) function openEnquiryForm(productId, productName) { // If you have existing modal code, use that // Otherwise, create/show modal // Example - adjust based on your current implementation: var modal = $('#enquiry-modal'); // or however you're calling your modal if (modal.length) { // Pre-fill product name in subject field if exists modal.find('input[name="product_name"]').val(productName); modal.find('input[name="product_id"]').val(productId); // Show modal (adjust based on your modal plugin) modal.fadeIn(); // OR // modal.modal('show'); // If using Bootstrap } } // Close enquiry modal (if needed) $(document).on('click', '.close-modal, .modal-backdrop', function () { $('#enquiry-modal').fadeOut(); }); });