//nopCommerce 4.90 shows "-THIS PRODUCT IS SOLD OUT-" until an attribute value is selected //("Allow only existing attribute combinations" + "Disable" display type), so select the first in-stock value document.addEventListener('DOMContentLoaded', function () { $(function () { Object.keys(window).forEach(function (key) { var match = /^combinationsBehavior_(\d+)$/.exec(key); var behavior = match && window[key]; if (!behavior || !behavior.settings) return; function selectFirstAvailableValues() { var selected = 0; $('[data-attr]', behavior.settings.contentEl).each(function () { var $attribute = $(this); var $first = $attribute.find('[data-attr-value]:not(.disabled)').first(); if (!$first.length) return; if ($attribute.is('select')) { if ($attribute.val() === '0') { $attribute.val(String($first.data('attr-value'))).trigger('change'); selected++; } } else { var $radios = $attribute.find('input[type=radio]'); if ($radios.length && !$radios.is(':checked')) { $first.find('input[type=radio]')[0].click(); selected++; } } }); //several attributes => several requests; refresh once more so the last answer is the full selection var refresh = window['attribute_change_handler_' + match[1]]; if (selected > 1 && refresh) $(document).one('ajaxStop', refresh); } //wait until sold-out values are greyed out and the first "-THIS PRODUCT IS SOLD OUT-" requests are finished, //otherwise a late answer can overwrite the correct text function run() { if ($.active) $(document).one('ajaxStop', selectFirstAvailableValues); else selectFirstAvailableValues(); } if (behavior.params.availableCombinations.length) run(); else $(behavior).one('processed', run); }); }); });