Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@corpsefilth
Created April 16, 2015 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corpsefilth/e95be5d38b52666e7930 to your computer and use it in GitHub Desktop.
Save corpsefilth/e95be5d38b52666e7930 to your computer and use it in GitHub Desktop.
Configurable products, make first dropdown option selected by default -- does not work with configurable swatches, only default configurable dropdown.
<script>
//we create new function
spConfig.setInitialState = function(dropdown_id) {
//select dropdown
var dropdown = $(dropdown_id);
//remove empty option from dropdown so it is not selectable after initial selection
dropdown[0].remove();
//change selections in dropdowns
for(index = 0; index < dropdown.length; index++) {
if(dropdown[index].value != "") {
dropdown.selectedIndex = index;
var element = dropdown;
var event = 'change';
//fire events
if(document.createEventObject) {
var evt = document.createEventObject();
return element.fireEvent('on'+event,evt);
} else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true );
return !element.dispatchEvent(evt);
}
}
}
};
<?php foreach($_attributes as $_attribute): ?>
spConfig.setInitialState("attribute<?php echo $_attribute->getAttributeId() ?>")
<?php endforeach; ?>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment