jQuery(document).ready(function ($) {
// Listen for changes in the size dropdown
$('select[name="attribute_pa_size"]').on('change', function () {
var selectedSize = $(this).val(); // Get the selected size
var productId = $('input[name="add-to-cart"]').val(); // Get the product ID
// Ensure both values are present
if (!selectedSize || !productId) {
console.error("Missing selected size or product ID");
return;
}
// Send the selected size to the server via AJAX
$.ajax({
url: '/wp-admin/admin-ajax.php', // AJAX handler URL
type: 'POST',
data: {
action: 'update_selected_size', // Action defined in PHP
selected_size: selectedSize,
product_id: productId
},
success: function (response) {
if (response.success) {
console.log('Size updated successfully:', response.data.selected_size);
} else {
console.error('Failed to update size:', response.data.message);
}
},
error: function (xhr, status, error) {
console.error('AJAX Error:', error);
}
});
});
});