1function applyStock($wrap, context) { 2 const infoKey = Object.keys(window).find(function (k) { 3 return /^productSalePointsInfo_\d+$/.test(k); 4 }); 5 6 if (!infoKey) return; 7 const info = window[infoKey]; 8 if (!info || !info.hasVariations || !info.variationSwatchesData) return; 9 10 const variations = Object.values(info.variationSwatchesData).map(v => { 11 return { 12 attributes: v.variationSwatches, 13 is_in_stock: v.variationSwatches.stock > 0 14 }; 15 }); 16 17 $wrap.find('.variations_form').each(function() { 18 const $form = jQuery(this); 19 const firstRun = !$form.data('dbi_stock_processed'); 20 21 if (firstRun) { 22 $form.find('select').each(function() { 23 jQuery(this).val(''); 24 }); 25 } 26 27 $form.find('select option') 28 .prop('disabled', false) 29 .removeClass('variation-option-out-of-stock disabled'); 30 $form.find('.button-variable-item') 31 .removeClass('disabled'); 32 33 if (!Array.isArray(variations) || variations.length === 0) { 34 return; 35 } 36 37 const currentSelection = {}; 38 $form.find('select').each(function() { 39 const name = jQuery(this).attr('name').replace(/^attribute_/, ''); 40 currentSelection[name] = jQuery(this).val(); 41 }); 42 43 44 if (firstRun) { 45 $form.data('dbi_stock_processed', true); 46 47 const hasSwatchAttr = $form.find('.button-variable-item').length > 0; 48 if (hasSwatchAttr) { 49 $form.find('select').val('').trigger('change'); 50 $form.find('.button-variable-item').removeClass('selected'); 51 } 52 } 53 54 const attributeNames = Object.keys(variations[0].attributes).filter(k => k !== "stock"); 55 56 const multiWithSwatch = attributeNames.length > 1 && attributeNames.some(a => 57 $form.find('.button-variable-item[data-attribute_name="' + a + '"]').length > 0 58 ); 59 60 attributeNames.forEach(attrName => { 61 const isColorAttr = attrName.toLowerCase().includes('color'); 62 const skipDisableForColor = attributeNames.length > 1 && isColorAttr; 63 const currentSelectedForAttr = $form.find('select[name="attribute_' + attrName + '"]').val(); 64 65 $form.find('select[name="attribute_' + attrName + '"] option').each(function() { 66 const $option = jQuery(this); 67 const optVal = $option.val(); 68 if (!optVal) return; 69 70 const possible = variations.filter(v => String(v.attributes[attrName]) === String(optVal)); 71 const filtered = possible.filter(v => 72 Object.entries(currentSelection).every(([key, sel]) => 73 key === attrName || !sel || v.attributes[key] === sel 74 ) 75 ); 76 77 let shouldDisable = false; 78 if (filtered.length === 0) { 79 shouldDisable = true; 80 } else if (firstRun && multiWithSwatch) { 81 shouldDisable = false; 82 } else if (firstRun) { 83 shouldDisable = filtered.every(v => !v.is_in_stock); 84 } else { 85 shouldDisable = !filtered.some(v => v.is_in_stock); 86 } 87 88 if (attributeNames.length > 1 && attrName.toLowerCase().includes('color')) { 89 shouldDisable = false; 90 } 91 92 if (!skipDisableForColor && shouldDisable && String(optVal) !== String(currentSelectedForAttr)) { 93 $option.addClass('variation-option-out-of-stock'); 94 } else if (String(optVal) === String(currentSelectedForAttr)) { 95 $option.removeClass('variation-option-out-of-stock'); 96 } 97 }); 98 99 $form.find('li[data-attribute_name="attribute_' + attrName + '"]') 100 .each(function() { 101 const $item = jQuery(this); 102 const swVal = $item.data('value'); 103 104 const possible = variations.filter(v => String(v.attributes[attrName]) === String(swVal)); 105 const filtered = possible.filter(v => 106 Object.entries(currentSelection).every(([key, sel]) => 107 key === attrName || !sel || v.attributes[key] === sel 108 ) 109 ); 110 111 const stockCheckSet = (firstRun && multiWithSwatch) ? possible : filtered; 112 113 let shouldDisableSwatch = false; 114 if (filtered.length === 0) { 115 shouldDisableSwatch = true; 116 } else if (firstRun && multiWithSwatch) { 117 shouldDisableSwatch = !stockCheckSet.some(v => v.is_in_stock); 118 } else if (firstRun) { 119 shouldDisableSwatch = stockCheckSet.every(v => !v.is_in_stock); 120 } else { 121 shouldDisableSwatch = !stockCheckSet.some(v => v.is_in_stock); 122 } 123 124 if (attributeNames.length > 1 && attrName.toLowerCase().includes('color')) { 125 shouldDisableSwatch = false;
126 } 127 128 if (!skipDisableForColor && shouldDisableSwatch && String(swVal) !== String(currentSelectedForAttr) && !$item.hasClass('selected')) { 129 $item.addClass('disabled'); 130 } else if (String(swVal) === String(currentSelectedForAttr) || $item.hasClass('selected')) { 131 $item.removeClass('disabled'); 132 } 133 }); 134 }); 135 136 attributeNames.forEach(attrName => { 137 const selectedVal = $form.find('select[name="attribute_' + attrName + '"]').val(); 138 if (selectedVal) { 139 $form.find('li[data-attribute_name="attribute_' + attrName + '"][data-value="' + selectedVal + '"], .button-variable-item[data-attribute_name="' + attrName + '"][data-value="' + selectedVal + '"]') 140 .addClass('selected'); 141 } 142 }); 143 }); 144} 145 146jQuery(document).on('found_variation reset_data', function(e) { 147 const form = document.querySelector('.variations_form'); 148 const value = form.getAttribute('data-product_variations'); 149 150 if (value === "false") { 151 setTimeout(() => { 152 jQuery('ul.variable-items-wrapper .variable-item.selected, ul.variable-items-wrapper .button-variable-item.selected') 153 .removeClass('selected'); 154 155 applyStock(jQuery(document.body), 'woocommerce'); 156 }, 0); 157 } 158});
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.