Skip to content

Instantly share code, notes, and snippets.

@tiagomatos
Created February 20, 2018 12:16
Show Gist options
  • Save tiagomatos/948a6213c951e97df116318b01ff0271 to your computer and use it in GitHub Desktop.
Save tiagomatos/948a6213c951e97df116318b01ff0271 to your computer and use it in GitHub Desktop.
Select first variant with stock
$(document).ready(function() {
var parentId;
var parentOptionCounter = 0;
$.each($(".prod-options option"), function( index, option ) {
var stock = $(option).data("variant-stock");
if(index == 0 || parentId !== $(option).parent().attr('id')){
parentId = $(option).parent().attr('id');
parentOptionCounter = 0;
}
if(stock != '0' && parentOptionCounter > 0){
$(option).prop('selected', true).trigger('change'); // selecting the first non-stock zero option.
return false;
}
parentOptionCounter++;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment