Skip to content

Instantly share code, notes, and snippets.

@drabbytux
Created January 30, 2017 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drabbytux/bb1d936ca6828965887f7e159f50b55a to your computer and use it in GitHub Desktop.
Save drabbytux/bb1d936ca6828965887f7e159f50b55a to your computer and use it in GitHub Desktop.
Hide variants that are sold out theme.js portion
/* Remove variants that are sold out in the dropdown */
jQuery(document).ready(function(){
if(typeof arr_titles_to_remove != 'undefined' ){
var $addToCartForm = $('form[action="/cart/add"]');
var i_title;
for (i_title = 0; i_title < arr_titles_to_remove.length; ++i_title) {
jQuery('.single-option-selector option').filter(function() { return jQuery(this).text() === arr_titles_to_remove[i_title] }).remove();
}
jQuery('.single-option-selector').trigger('change');
if (window.MutationObserver && $addToCartForm.length) {
if (typeof observer === 'object' && typeof observer.disconnect === 'function') {
observer.disconnect();
}
var config = { childList: true, subtree: true };
var observer = new MutationObserver(function() {
console.log('test 2');
var i_title;
for (i_title = 0; i_title < arr_titles_to_remove.length; ++i_title) {
jQuery('.single-option-selector option').filter(function() { return jQuery(this).text() === arr_titles_to_remove[i_title] }).remove();
}
jQuery('.single-option-selector').trigger('change');
observer.disconnect();
});
observer.observe($addToCartForm[0], config);
}
}
});
@mvahle
Copy link

mvahle commented Mar 8, 2017

Will this not work in a sectioned theme?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment