-
-
Save Ndeeri/202e62f99eef14e17047365f8ee4cd6c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| // Text to match for the placeholder option | |
| var PLACEHOLDER_TEXT = 'Please select an option'; | |
| // Selectors for the add-to-cart button(s). Adjust if the theme uses | |
| // a different selector (confirm in DevTools before changing). | |
| var ADD_TO_CART_SELECTORS = [ | |
| 'button[name="add"]', | |
| 'input[name="add"]', | |
| 'button[type="submit"][name="add"]', | |
| '.product-form__cart-submit', | |
| '.product-form__submit' | |
| ].join(', '); | |
| function setPlaceholderAsDefault(root) { | |
| var selects = (root || document).querySelectorAll('select'); | |
| selects.forEach(function (select) { | |
| // Skip if we already processed this select | |
| if (select.dataset.sbPlaceholderApplied === 'true') return; | |
| // Find the "Please select an option" option by text | |
| var options = Array.prototype.slice.call(select.options || []); | |
| var placeholderOption = options.find(function (opt) { | |
| if (!opt || typeof opt.text !== 'string') return false; | |
| return opt.text.trim().toLowerCase() === PLACEHOLDER_TEXT.toLowerCase(); | |
| }); | |
| if (!placeholderOption) return; | |
| // Make sure it is selectable | |
| placeholderOption.disabled = false; | |
| // Set as selected | |
| select.value = placeholderOption.value; | |
| // Mark as processed so we do not keep resetting the select | |
| select.dataset.sbPlaceholderApplied = 'true'; | |
| // [SB Debug] When this select changes, re-check the button state | |
| select.addEventListener('change', updateAddToCartState); | |
| // Trigger change event so Infinite Options / theme JS reacts correctly | |
| var evt = new Event('change', { bubbles: true }); | |
| select.dispatchEvent(evt); | |
| }); | |
| } | |
| // Returns true if any tracked select still has the placeholder selected | |
| function hasPlaceholderSelected() { | |
| var tracked = document.querySelectorAll( | |
| 'select[data-sb-placeholder-applied="true"]' | |
| ); | |
| for (var i = 0; i < tracked.length; i++) { | |
| var select = tracked[i]; | |
| var selectedOption = select.options[select.selectedIndex]; | |
| if (!selectedOption) continue; | |
| if (selectedOption.text.trim().toLowerCase() === PLACEHOLDER_TEXT.toLowerCase()) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| function updateAddToCartState() { | |
| var shouldDisable = hasPlaceholderSelected(); | |
| var buttons = document.querySelectorAll(ADD_TO_CART_SELECTORS); | |
| buttons.forEach(function (btn) { | |
| if (shouldDisable) { | |
| btn.setAttribute('disabled', 'disabled'); | |
| btn.setAttribute('aria-disabled', 'true'); | |
| // Tag so we only re-enable buttons WE disabled | |
| btn.dataset.sbDisabledByBundle = 'true'; | |
| } else if (btn.dataset.sbDisabledByBundle === 'true') { | |
| btn.removeAttribute('disabled'); | |
| btn.removeAttribute('aria-disabled'); | |
| delete btn.dataset.sbDisabledByBundle; | |
| } | |
| }); | |
| console.log('[SB Debug] placeholder selected:', shouldDisable, | |
| '| buttons checked:', buttons.length); | |
| } | |
| function init() { | |
| setPlaceholderAsDefault(document); | |
| updateAddToCartState(); | |
| // Watch for new dropdowns injected by Infinite Options / bundles | |
| var observer = new MutationObserver(function (mutations) { | |
| var sawChanges = false; | |
| mutations.forEach(function (mutation) { | |
| if (!mutation.addedNodes || mutation.addedNodes.length === 0) return; | |
| mutation.addedNodes.forEach(function (node) { | |
| if (node.nodeType !== 1) return; // element only | |
| sawChanges = true; | |
| if (node.tagName === 'SELECT') { | |
| setPlaceholderAsDefault(node); | |
| } else { | |
| setPlaceholderAsDefault(node); | |
| } | |
| }); | |
| }); | |
| // If anything was added (new selects or a new add-to-cart button), | |
| // re-evaluate state. | |
| if (sawChanges) updateAddToCartState(); | |
| }); | |
| observer.observe(document.body, { | |
| childList: true, | |
| subtree: true | |
| }); | |
| } | |
| if (document.readyState === 'loading') { | |
| document.addEventListener('DOMContentLoaded', init); | |
| } else { | |
| init(); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment