Skip to content

Instantly share code, notes, and snippets.

@jstneti01
Created January 15, 2017 15:06
Show Gist options
  • Save jstneti01/3701e6848eba35199485e71fd715c366 to your computer and use it in GitHub Desktop.
Save jstneti01/3701e6848eba35199485e71fd715c366 to your computer and use it in GitHub Desktop.
Example of how to create a taxonomy dropdown filtered by WooCommerce Product Category we are currently on.
<?php
//Put this code in your child theme inside the woocommerce folder or add it with a filter.
// Get currenty category ID
$cat_id = get_queried_object_id();
// Get Location terms filtered by Product Category of this page.
$post_ids = get_objects_in_term( $cat_id, 'product_cat' ); // WooCommerce default category
$terms = wp_get_object_terms( $post_ids, 'location' ); // Custom Taxonomy for WooCommerce Products
// Put them in an array like this: [slug]=>"name"
$terms = array_combine(
wp_list_pluck( $terms, 'slug' ),
wp_list_pluck( $terms, 'name' )
);
?>
<select name="location" id="location">
<?php
foreach($terms as $key => $term) {
echo '<option value="' . $key . '">' . $term . '</option>';
}
?>
</select>
<input class="SubmitButton" type="button" value="Show" onclick="showProducts()"/>
function showProducts() {
var loc = document.getElementById('location');
var option = loc.options[loc.selectedIndex].value;
location.href = window.location.href.split(/\?|#/)[0] + '?location=' + option;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment