1(function ($) { 2 3 function serializeForm(form, submitter) { 4 var formData = new FormData(form, submitter); 5 var serializedObject = {}; 6 7 formData.append('product_id', $(submitter).val()); 8 9 formData.forEach(function(value, key) { 10 if (key.includes('[')) { 11 var keys = key.split(/\[|\]/).filter(function(k) { return k; }); 12 var current = serializedObject; 13 14 keys.forEach(function(subKey, index) { 15 if (index === keys.length - 1) { 16 if (Array.isArray(current[subKey])) { 17 current[subKey].push(value); 18 } else if (current[subKey]) { 19 current[subKey] = [current[subKey], value]; 20 } else { 21 current[subKey] = key.includes('[]') ? [value] : value; // Key includes [] without a subkey, ie: field[]. 22 } 23 } else { 24 if (!current[subKey]) { 25 current[subKey] = {}; 26 } 27 current = current[subKey]; 28 } 29 }); 30 } else { 31 if (serializedObject[key]) { 32 if (Array.isArray(serializedObject[key])) { 33 serializedObject[key].push(value); 34 } else { 35 serializedObject[key] = [serializedObject[key], value]; 36 } 37 } else { 38 serializedObject[key] = value; 39 } 40 } 41 }); 42 43 // Check if product_id exists, if not add it with the value of add-to-cart. Use variation_id for variable products. 44 if (serializedObject['variation_id']) { 45 serializedObject['product_id'] = serializedObject['variation_id']; 46 } else if (!serializedObject['product_id'] && serializedObject['add-to-cart']) { 47 serializedObject['product_id'] = serializedObject['add-to-cart']; 48 } 49 50 delete serializedObject['add-to-cart']; // Need to remove this so that the form handler doesn't try to add the product to the cart again. 51 52 return serializedObject; 53 } 54 55 $(document).on('click', '.single_add_to_cart_button:not(.disabled)', function (e) { 56 57 var $thisbutton = $(this), 58 $form = $thisbutton.closest('form.cart'), 59 data = serializeForm( $form[0], this ); 60 61 e.preventDefault(); 62 63 $(document.body).trigger('adding_to_cart', [$thisbutton, data]); 64 65 $.ajax({ 66 type: 'POST', 67 url: woocommerce_params.wc_ajax_url.toString().replace('%%endpoint%%', 'add_to_cart'), 68 data: data, 69 beforeSend: function (response) { 70 $thisbutton.prop( 'disabled', true ); 71 $thisbutton.removeClass('added').addClass('loading'); 72 }, 73 complete: function (response) { 74 $thisbutton.prop( 'disabled', false ); 75 $thisbutton.addClass('added').removeClass('loading'); 76 }, 77 success: function (response) { 78 79 if (response.error && response.product_url) { 80 window.location = response.product_url; 81 return; 82 } 83 84 $(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash, $thisbutton]); 85 }, 86 }); 87 88 return false; 89 90 }); 91})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.