1//console.log('k2 checkout script loaded'); 2 3const CART_STORE = 'wc/store/cart'; 4 5const excludedIds = [ 6 51853, 7 51856, 8 51859, 9 51860, 10 51863, 11 51864, 12 52213, 13 52212, 14 52211, 15 52210, 16 52209, 17 52734, 18 52746, 19 52747, 20 52749, 21 52750, 22 52752, 23 52753, 24 52754, 25 52755, 26 52756 27]; 28 29function canUseCheckoutUpdate() { 30 return ( 31 window.wc && 32 window.wc.blocksCheckout && 33 typeof window.wc.blocksCheckout.extensionCartUpdate === 'function' 34 ); 35} 36 37function pushGiftWrap(checked) { 38 if (!canUseCheckoutUpdate()) { 39 return; 40 } 41 42 window.wc.blocksCheckout.extensionCartUpdate({ 43 namespace: 'k2', 44 data: { gift_wrap: !!checked } 45 }).catch(function (e) { 46 console.log(e); 47 }); 48} 49 50function removeField($field) { 51 if (!$field.length) { 52 return; 53 } 54 55 const $label = $field.siblings('label'); 56 57 $field.val(''); 58 $field.trigger('change'); 59 60 $label.remove(); 61 $field.remove(); 62} 63 64function updateCartProductIds() { 65 if (!window.wp || !window.wp.data) { 66 return false; 67 } 68 69 const checkoutFields = document.querySelector( 70 '.wp-block-woocommerce-checkout-additional-information-block' 71 ); 72 73 if (!checkoutFields) { 74 return false; 75 } 76 77 const cartStore = window.wp.data.select(CART_STORE); 78 79 if (!cartStore || typeof cartStore.getCartData !== 'function') { 80 return false; 81 } 82 83 const cart = cartStore.getCartData(); 84 85 if (!cart || !Array.isArray(cart.items)) { 86 return false; 87 } 88 89 const productIds = cart.items.map(function (item) { 90 return item.id; 91 }); 92 93 if (!productIds.length) { 94 return false; 95 } 96 97 const selectorPart = productIds 98 .map(function (id) { 99 return '[data-ids*="' + id + '"]'; 100 }) 101 .join(','); 102 103 const notRequiredInputs = checkoutFields.querySelectorAll( 104 'input:not(' + selectorPart + ')' 105 ); 106 107 const notRequiredSelects = checkoutFields.querySelectorAll( 108 'select:not(' + selectorPart + ')' 109 ); 110 111 jQuery(notRequiredInputs).each(function () { 112 const $input = jQuery(this); 113 114 if ($input.attr('id') === 'order-k2-gift_wrap') { 115 return; 116 } 117 118 removeField($input); 119 }); 120 121 jQuery(notRequiredSelects).each(function () { 122 removeField(jQuery(this)); 123 }); 124 125 const shouldRemoveGiftWrap = productIds.some(function (id) { 126 return excludedIds.includes(id); 127 }); 128 129 if (shouldRemoveGiftWrap) { 130 const $giftWrap = jQuery('#order-k2-gift_wrap'); 131 132 removeField($giftWrap); 133 134 jQuery('.wc-block-components-address-form__k2-gift_wrap').hide(); 135 136 if (canUseCheckoutUpdate()) { 137 window.wc.blocksCheckout.extensionCartUpdate({ 138 namespace: 'k2', 139 data: { gift_wrap: false } 140 }).catch(function (e) { 141 console.log(e); 142 }); 143 } 144 } 145 146 return true; 147} 148 149function wrapLogic(el) { 150 if (!el || el.dataset.k2GiftWrapBound) { 151 return; 152 } 153 154 el.dataset.k2GiftWrapBound = 'true'; 155 156 pushGiftWrap(el.checked); 157 158 el.addEventListener('change', function (e) { 159 pushGiftWrap(e.target.checked); 160 }); 161} 162 163function init() { 164 if (!window.wp || !window.wp.data) { 165 return; 166 } 167 168 const giftWrap = document.querySelector('#order-k2-gift_wrap'); 169 170 if (giftWrap) { 171 wrapLogic(giftWrap); 172 } 173 174 updateCartProductIds(); 175} 176 177window.addEventListener('load', function () { 178 if (!window.wp || !window.wp.data) { 179 return; 180 } 181 182 const unsubscribe = window.wp.data.subscribe(function () { 183 if (updateCartProductIds()) { 184 unsubscribe(); 185 } 186 }); 187 188 const observer = new MutationObserver(function () { 189 init(); 190 }); 191 192 observer.observe(document.documentElement, { 193 childList: true, 194 subtree: true 195 }); 196 197 init(); 198}); 199/*const { select, subscribe } = wp.data; 200console.log("test"); 201// Store key for cart data 202const CART_STORE = 'wc/store/cart'; 203 204// Function to grab product IDs and do stuff 205function updateCartProductIds() { 206 //get cart's data 207 const cart = select(CART_STORE).getCartData(); 208 let green_1_id = 51853; 209 let green_2_id = 51856; 210 let explorer_1_4_id = 51859; 211 let explorer_2_4_id = 51860; 212 let naturalist_id = 51863; 213 let protector_id = 51864; 214 215 let green_2_en_id = 52213; 216 let explorer_1_4_en_id = 52212; 217 let explorer_2_4_en_id = 52211; 218 let naturalist_en_id = 52210; 219 let protector_en_id = 52209; 220 221 let ids = [ 222 green_1_id, 223 green_2_id, 224 explorer_1_4_id, 225 explorer_2_4_id, 226 naturalist_id, 227 protector_id, 228 green_2_en_id, 229 explorer_1_4_en_id, 230 explorer_2_4_en_id, 231 naturalist_en_id, 232 protector_en_id, 233 52734, 234 52746, 235 52747, 236 52749, 237 52750, 238 52752, 239 52753, 240 52754, 241 52755, 242 52756 243 ]; 244 245 if (cart && cart.items) { 246 //take the product ids 247 const product_id = (cart.items.map(item => item.id))[0]; 248 if (product_id) { 249 //pick the inputs and selects that don't have inside the data-ids attribute the product_id we are in the loop 250 //so these inputs are not required for the order. 251 let not_required_inputs = document.querySelectorAll('.wp-block-woocommerce-checkout-additional-information-block input:not([data-ids*="' + product_id + '"])'); 252 let not_required_select = document.querySelectorAll('.wp-block-woocommerce-checkout-additional-information-block select:not([data-ids*="' + product_id + '"])'); 253 254 //remove only the inputs and labels, because woocommerce throws an error if we remove the whole div of every field 255 if (not_required_inputs.length) { 256 jQuery(not_required_inputs).each(function() { 257 let input = jQuery(this); 258 if (jQuery(input).attr('id') == 'order-k2-gift_wrap') return; 259 let label = jQuery(this).siblings('label'); 260 jQuery(input).prop('value', ''); 261 jQuery(input).trigger('change'); 262 jQuery(label).remove(); 263 jQuery(input).remove(); 264 265 }); 266 } 267 268 //same with above 269 if (not_required_select.length) { 270 jQuery(not_required_select).each(function() { 271 let input = jQuery(this); 272 let label = jQuery(this).siblings('label'); 273 jQuery(input).prop('value', ''); 274 jQuery(input).trigger('change'); 275 jQuery(label).remove(); 276 jQuery(input).remove(); 277 }); 278 } 279 280 if (ids.includes(product_id)) { 281 let input = jQuery('#order-k2-gift_wrap'); 282 let label = jQuery('#order-k2-gift_wrap').siblings('label'); 283 jQuery(input).prop('value', ''); 284 jQuery(input).trigger('change'); 285 jQuery(label).remove(); 286 jQuery(input).remove(); 287 jQuery('.wc-block-components-address-form__k2-gift_wrap').hide(); 288 wc.blocksCheckout.extensionCartUpdate({ 289 namespace: 'k2', 290 data: { gift_wrap: false } 291 }); 292 } 293 } 294 295 return true; 296 } 297 298 return false;
299} 300 301function push(checked) { 302 if (!window.wc || !wc.blocksCheckout || !wc.blocksCheckout.extensionCartUpdate) return; 303 304 wc.blocksCheckout.extensionCartUpdate({ 305 namespace: 'k2', 306 data: { gift_wrap: !!checked } 307 }).catch(function(e) { 308 console.log(e); 309 }); 310} 311 312function wrapLogic(el) { 313 if (!el) return; 314 // Push current state so session & totals align (covers refresh) 315 push(el.checked); 316 el.addEventListener('change', function(e) { 317 push(e.target.checked); 318 }); 319} 320 321window.addEventListener('load', function() { 322 const unsubscribe = wp.data.subscribe(() => { 323 if (updateCartProductIds()) { 324 unsubscribe(); // Stop listening once loaded 325 } 326 }); 327 // Update whenever cart changes 328 // subscribe(() => { 329 // updateCartProductIds(); 330 // }); 331 332 // The Checkout Block renders asynchronously â observe then bind. 333 var mo = new MutationObserver(function() { 334 let el = document.querySelector('input[id="order-k2-gift_wrap"]'); 335 336 if (el) { 337 wrapLogic(el); 338 mo.disconnect(); 339 } 340 }); 341 mo.observe(document.documentElement, { 342 childList: true, 343 subtree: true 344 }); 345}); 346*/
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.