1(function ($) { 2 'use strict'; 3 4 /** 5 * This script is used to handle the classic cart and checkout based optional fees functionality 6 */ 7 $(document).ready(function ($) { 8 // Clear localStorage if the cart is empty 9 $(document.body).on('wc_cart_emptied', function () { 10 localStorage.removeItem('wcpfc_browser_data'); 11 localStorage.removeItem('wcpfc_cart_hand_checked'); 12 }); 13 14 // Event listeners for fee changes on cart and checkout pages 15 $(document).on('change', '.woocommerce-cart .optional_fee_container input[type="checkbox"], .woocommerce-cart .optional_fee_container select, .woocommerce-cart .optional_fee_container input[type="radio"]', function () { 16 classicAddFeeDataInSession(); 17 dropdown_check(); 18 }); 19 20 $(document).on('change', '.woocommerce-checkout .optional_fee_container input[type="checkbox"], .woocommerce-checkout .optional_fee_container select, .woocommerce-checkout .optional_fee_container input[type="radio"]', function () { 21 classicAddFeeDataInSession(); 22 dropdown_check(); 23 }); 24 25 // If the checkout page is present, set the data from the localStorage to the fields 26 if( $('.woocommerce-checkout').length > 0 ) { 27 28 let customDataApplied = false; 29 30 $(document.body).on('updated_checkout', function () { 31 32 // If the data has already been applied, return 33 if (customDataApplied) { 34 return; 35 } 36 37 // Set the data as applied 38 customDataApplied = true; 39 40 if (localStorage.getItem('wcpfc_browser_data') !== null ) { 41 classicUpdateFieldsBasedOnStoredData(); 42 } 43 44 // We first need to set the data in the localStorage before updating the fields based on the stored data 45 if ($('.woocommerce-checkout .optional_fee_container').length > 0 ) { 46 classicAddFeeDataInSession(); 47 dropdown_check(); 48 } 49 }); 50 } 51 52 // If the cart page is present, set the data from the localStorage to the fields 53 if( $('.woocommerce-cart').length > 0 ) { 54 55 if (localStorage.getItem('wcpfc_browser_data') !== null ) { 56 classicUpdateFieldsBasedOnStoredData(); 57 } 58 if ($('.woocommerce-cart .optional_fee_container').length > 0 ) { 59 classicAddFeeDataInSession(); 60 dropdown_check(); 61 } 62 } 63 }); 64 65 /** 66 * Functions for the classic cart and checkout based optional fees functionality 67 */ 68 // Block UI 69 function classicBlockUI() { 70 $('.woocommerce-checkout .woocommerce-checkout-payment, .woocommerce-checkout .shop_table, .woocommerce-cart .optional_fee_container').block({ 71 message: null, 72 overlayCSS: { background: '#fff', opacity: 0.6 } 73 }); 74 } 75 76 // Unblock UI 77 function classicUnblockUI() { 78 $('.woocommerce-checkout .woocommerce-checkout-payment, .woocommerce-checkout .shop_table, .woocommerce-cart .optional_fee_container').unblock(); 79 } 80 81 // Update fields based on stored data 82 function classicUpdateFieldsBasedOnStoredData() { 83 84 if (window.wc && window.wc.blocksCheckout) { 85 return; 86 } 87 88 const storedData = JSON.parse(localStorage.getItem('wcpfc_browser_data')) || []; 89 90 $('.optional_fee_container input[type="checkbox"], .optional_fee_container input[type="radio"]').each(function () { 91 updateInputState($(this), storedData); 92 }); 93 94 $('.optional_fee_container select').each(function () { 95 const selectedValue = parseInt($(this).data('value')); 96 $(this).val(storedData.includes(selectedValue) ? 'yes' : 'no'); 97 }); 98 99 dropdown_check(); 100 } 101 102 // State change of element 103 function updateInputState(inputElement, storedData) { 104 105 // Determine if fee id exists in storedData (fee applied) 106 const feeId = parseInt(inputElement.val()); 107 const feeApplied = storedData.includes(feeId); 108 109 if (inputElement.attr('type') === 'checkbox') { 110 // For checkbox: checked if fee applied, unchecked otherwise 111 inputElement.prop('checked', feeApplied); 112 } else if (inputElement.attr('type') === 'radio') { 113 // For radio: select 'Yes' if applied, 'No' if not 114 // Radio 'yes' value is typically 'yes', 'no' is typically 'no' 115 if (inputElement.val().toLowerCase() === 'yes') { 116 inputElement.prop('checked', feeApplied); 117 } else if (inputElement.val().toLowerCase() === 'no') { 118 inputElement.prop('checked', !feeApplied); 119 } 120 } 121 } 122 123 // Check if the dropdown is checked 124 function dropdown_check() { 125 $('.input-dropdown').each(function () { 126 $(this).next(':hidden').remove(); 127 if ('yes' === $(this).val() || 'Yes' === $(this).val()) { 128 $('<input>').attr({ 129 type: 'hidden', 130 name: 'wef_fees_id_array_' + $(this).data('value') + '[]', 131 value: $(this).data('value'), 132 }).insertAfter($(this)); 133 } 134 }); 135 } 136 137 // Add fee data to session classic method
138 function classicAddFeeDataInSession() { 139 140 if (window && window.wc && window.wc.blocksCheckout) { 141 return; 142 } 143 144 const isCartPage = $('.woocommerce-cart').length > 0; 145 const isCheckoutPage = $('.woocommerce-checkout').length > 0; 146 147 if (!isCartPage && !isCheckoutPage) { return; } 148 149 classicBlockUI(); 150 151 let sendData = []; 152 const data = classicCollectFeeData(); // Get the current fee data 153 154 var checkbox; 155 var radio; 156 var dropdown; 157 var dropdownValue; 158 159 if (isCartPage) { 160 checkbox = $('.woocommerce-cart .optional_fee_container').find('input[type="checkbox"]:checked'); 161 radio = $('.woocommerce-cart .optional_fee_container').find('input[type="radio"]:checked'); 162 dropdown = $('.woocommerce-cart .optional_fee_container').find('select'); 163 dropdownValue = (dropdown.length > 0) ? dropdown.val() : undefined; 164 165 if (checkbox.is(':checked') || radio.val() !== '' || (dropdownValue !== undefined && dropdownValue.toLowerCase() === 'yes')) { 166 localStorage.setItem('wcpfc_browser_data', JSON.stringify(data)); 167 localStorage.setItem('wcpfc_cart_hand_checked', true); 168 sendData = data; 169 } else { 170 localStorage.removeItem('wcpfc_browser_data'); 171 localStorage.setItem('wcpfc_cart_hand_checked', false); 172 sendData = []; 173 } 174 175 if ($('.cart_item').length > 0) { 176 177 // Send the array of fee IDs via AJAX 178 // Optimized AJAX request with error handling 179 try { 180 $.post( woocommerce_params.ajax_url, { 181 action: 'wcpfc_pro_cart_optional_fees_ajax__premium_only', 182 fees_ids: sendData 183 }).done(function(response) { 184 if (response) { 185 $(document.body).trigger('wc_update_cart'); 186 } 187 classicUnblockUI(); 188 }).fail(function(jqXHR, textStatus, errorThrown) { 189 console.error('Optional fees AJAX (cart) request failed:', textStatus, errorThrown); 190 classicUnblockUI(); 191 }); 192 } catch (err) { 193 console.error('An unexpected error occurred while sending optional fees (cart):', err); 194 classicUnblockUI(); 195 } 196 } 197 } 198 if (isCheckoutPage) { 199 sendData = JSON.parse(localStorage.getItem('wcpfc_browser_data')) || []; 200 var cartChecked = localStorage.getItem('wcpfc_cart_hand_checked'); 201 if (cartChecked !== null && cartChecked === 'true') { 202 localStorage.setItem('wcpfc_browser_data', JSON.stringify(data)); 203 localStorage.setItem('wcpfc_cart_hand_checked', true); 204 sendData = data; 205 } else { 206 checkbox = $('.woocommerce-checkout .optional_fee_container').find('.input-checkbox'); 207 radio = $('.woocommerce-checkout .optional_fee_container').find('input[type="radio"]:checked'); 208 dropdown = $('.woocommerce-checkout .optional_fee_container').find('select'); 209 dropdownValue = (dropdown.length > 0) ? dropdown.val() : undefined; 210 if (checkbox.is(':checked') || radio.val() !== '' || (dropdownValue !== undefined && dropdownValue.toLowerCase() === 'yes')) { 211 localStorage.setItem('wcpfc_browser_data', JSON.stringify(data)); 212 localStorage.setItem('wcpfc_cart_hand_checked', true); 213 sendData = data; 214 } else { 215 localStorage.removeItem('wcpfc_browser_data'); 216 localStorage.setItem('wcpfc_cart_hand_checked', false); 217 sendData = []; 218 } 219 } 220 // Send the array of fee IDs via AJAX 221 try { 222 $.post( woocommerce_params.ajax_url, { 223 action: 'wcpfc_pro_cart_optional_fees_ajax__premium_only', 224 fees_ids: sendData 225 }).done(function(response) { 226 if (response) { 227 setTimeout(function () { 228 $(document.body).trigger('update_checkout'); 229 classicUnblockUI(); 230 }, 200); 231 } 232 }).fail(function(jqXHR, textStatus, errorThrown) { 233 console.error('Optional fees AJAX request failed:', textStatus, errorThrown); 234 classicUnblockUI(); 235 }); 236 } catch (err) { 237 console.error('An unexpected error occurred while sending optional fees:', err); 238 classicUnblockUI(); 239 } 240 } 241 } 242 243 // Collect fee data from checkboxes, selects, and radios classic method 244 function classicCollectFeeData() { 245 246 const data = []; 247 $('.optional_fee_container') 248 .find('input[type="checkbox"]:checked, select, input[type="radio"]:checked') 249 .each(function () { 250 const $this = $(this); 251 const value = $this.val(); // Value of the input, select, or radio 252 253 if ($this.is('input[type="checkbox"]')) { 254 // For checkboxes, we expect a numeric value (fee ID) 255 if (value && !isNaN(value)) { 256 data.push(parseInt(value, 10)); 257 } 258 } else if ($this.is('select')) { 259 // For select inputs, check if the value is 'yes' 260 if (value.toLowerCase() === 'yes') { 261 const feeValue = $this.data('value'); // Using the data-value attribute 262 if (feeValue && !isNaN(feeValue)) { 263 data.push(parseInt(feeValue, 10)); 264 } 265 } 266 } else if ($this.is('input[type="radio"]')) { 267 // For radios, we expect a numeric value for 'Yes' option 268 if (value && !isNaN(value)) { 269 data.push(parseInt(value, 10)); 270 } 271 } 272 }); 273 274 return data; 275 } 276})(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.