Skip to content

Instantly share code, notes, and snippets.

@quimo
Last active October 4, 2016 16:04
Show Gist options
  • Save quimo/7b7f5bcba2221bea91bcd9a3d89c3961 to your computer and use it in GitHub Desktop.
Save quimo/7b7f5bcba2221bea91bcd9a3d89c3961 to your computer and use it in GitHub Desktop.
WP | Aggiunta di una select per una tassonomia gerarchica gestita dal plugin Search and Filters
jQuery(function(){
if (jQuery('body.home').length) {
var taxonomy = jQuery('li.sf-field-taxonomy-product_cat');
taxonomy.addClass('product_first_cat');
//clono la select della tassonomia e gli aggiungo una classe specifica
var taxonomy_clone = taxonomy.clone();
taxonomy_clone.removeClass('product_first_cat').addClass('product_second_cat');
taxonomy_clone.insertAfter(taxonomy);
//rimuovo le voci di secondo livello dalla prima select
taxonomy.find('option.sf-level-1').hide();
taxonomy_clone.find('option').hide();
jQuery('.product_first_cat select').on('change',function(){
var selected = jQuery(this).find('option:selected').attr('class');
jQuery('.product_second_cat option').hide();
jQuery('.product_second_cat option').each(function(){
if (jQuery(this).attr('class') == selected) jQuery(this).nextUntil('.sf-level-0').show();
});
});
jQuery('.product_second_cat select').on('change',function(){
jQuery('.product_first_cat select').val(jQuery(this).val());
});
}
});
@quimo
Copy link
Author

quimo commented Oct 4, 2016

Sul singolo form di ricerca in Search and Filters > General bisogna disabilitare le opzioni Enable auto count e Update the Search Form on user interaction altrimenti ad ogni interazione il form viene ricaricato via Ajax e l'aggiunta della select con jQuery è vanificata.

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