1function dbi_applyInStock($wrap, context) { 2 $wrap.find('.variations_form').each(function() { 3 const $form = jQuery(this); 4 5 const variations = $form.data('product_variations'); 6 7 $form.find('select option') 8 .prop('disabled', false) 9 .removeClass('variation-option-out-of-stock'); 10 $form.find('.button-variable-item') 11 .removeClass('disabled'); 12 13 if (!Array.isArray(variations)) { 14 return; 15 } 16 17 const currentSelection = {}; 18 $form.find('select').each(function() { 19 const name = jQuery(this).attr('name'); 20 currentSelection[name] = jQuery(this).val(); 21 }); 22 23 const firstRun = !$form.data('dbi_stock_processed'); 24 if (firstRun) { 25 $form.data('dbi_stock_processed', true); 26 27 const hasSwatchAttr = $form.find('.button-variable-item').length > 0; 28 if (hasSwatchAttr) { 29 $form.find('select').val('').trigger('change'); 30 $form.find('.button-variable-item').removeClass('selected'); 31 } 32 33 const $product = $form.closest('.woosb-product'); 34 $product.find('p.stock, .woosb-availability').hide(); 35 } 36 37 const attributeNames = Object.keys(variations[0].attributes); 38 39 const multiWithSwatch = attributeNames.length > 1 && attributeNames.some(a => 40 $form.find('.button-variable-item[data-attribute_name="' + a + '"]').length > 0 41 ); 42 43 attributeNames.forEach(attrName => { 44 const isColorAttr = attrName.toLowerCase().includes('color'); 45 const skipDisableForColor = attributeNames.length > 1 && isColorAttr; 46 47 $form.find('select[name="' + attrName + '"] option').each(function() { 48 const $option = jQuery(this); 49 const optVal = $option.val(); 50 51 if (!optVal) { 52 return; 53 } 54 55 const possible = variations.filter(v => String(v.attributes[attrName]) === String(optVal)); 56 const filtered = possible.filter(v => 57 Object.entries(currentSelection).every(([key, sel]) => 58 key === attrName || !sel || v.attributes[key] === sel 59 ) 60 ); 61 62 let shouldDisable = false; 63 if (filtered.length === 0) { 64 shouldDisable = true; 65 } else if (firstRun && multiWithSwatch) { 66 shouldDisable = false; 67 } else if (firstRun) { 68 shouldDisable = filtered.every(v => !v.is_in_stock); 69 } else { 70 shouldDisable = !filtered.some(v => v.is_in_stock); 71 } 72 73 if (!skipDisableForColor && shouldDisable) { 74 $option.prop('disabled', true).addClass('variation-option-out-of-stock'); 75 } 76 }); 77 78 $form.find('.button-variable-item[data-attribute_name="' + attrName + '"]') 79 .each(function() { 80 const $item = jQuery(this); 81 const swVal = $item.data('value'); 82 const possible = variations.filter(v => String(v.attributes[attrName]) === String(swVal)); 83 const filtered = possible.filter(v => 84 Object.entries(currentSelection).every(([key, sel]) => 85 key === attrName || !sel || v.attributes[key] === sel 86 ) 87 ); 88 const stockCheckSet = (firstRun && multiWithSwatch) ? possible : filtered; 89 90 let shouldDisableSwatch = false; 91 if (filtered.length === 0) { 92 shouldDisableSwatch = true; 93 } else if (firstRun && multiWithSwatch) { 94 shouldDisableSwatch = !stockCheckSet.some(v => v.is_in_stock); 95 } else if (firstRun) { 96 shouldDisableSwatch = stockCheckSet.every(v => !v.is_in_stock); 97 } else { 98 shouldDisableSwatch = !stockCheckSet.some(v => v.is_in_stock); 99 } 100 101 if (!skipDisableForColor && shouldDisableSwatch) { 102 $item.addClass('disabled'); 103 } 104 }); 105 }); 106 107 attributeNames.forEach(attrName => { 108 const selectedVal = $form.find('select[name="' + attrName + '"]').val(); 109 110 if (selectedVal) { 111 $form.find('li[data-attribute_name="' + attrName + '"][data-value="' + selectedVal + '"], .button-variable-item[data-attribute_name="' + attrName + '"][data-value="' + selectedVal + '"]') 112 .addClass('selected'); 113 } 114 }); 115 }); 116} 117 118jQuery(document).on('woosb_init', function(e, $wrap, context) { 119 if (context === 'ready') { 120 return; 121 } 122 123 dbi_applyInStock($wrap, context); 124});
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.