WooCommerce Product Category and Terms Select Filters [code snippet]


WooCommerce Product Category and Terms Select Filters [code snippet]

The code below will help you add a form select filter for Woocommerce online shop on WordPress.

<?php
$categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => true] ); ?>
<div class="product-categories">
    <h3>Categories</h3>
    <select class="form-control shop-filter" onchange="location = this.value;">
        <option selected value="/shop/">All</option>
        <?php foreach ( $categories as $category ) { ?>
        <option value="<?php echo get_term_link( $category->term_id  ); ?>"><?php echo $category->name; ?></option>
        <?php } ?>
      </select>
</div>

<?php
$terms = get_terms(array('taxonomy' => 'product_tag', 'hide_empty' => true)); ?>
<div class="product-tags" >
    <h3>Template Tags</h3>
    <select class="form-control shop-filter" onchange="location = this.value;">
        <option selected value="/shop/">All</option>
        <?php foreach ( $terms as $term ) { ?>
            <option value="<?php echo get_term_link( $term->term_id, 'product_tag' ); ?>"><?php echo $term->name; ?></option>
        <?php } ?>
      </select>
</div>

<script>
jQuery(document).ready(function() {
    var queryString = window.location.href;
    console.log(queryString);
    jQuery(".shop-filter > option").each(function() {
    if (this.value == queryString) {
    this.selected = 'selected';
    }
    });
});
</script>

 

 


About the Author

Jake Lett is a results-driven Detroit based B2B marketing consultant with 15+ years of hands-on experience managing SEO and PPC campaigns across manufacturing, SaaS, and professional services industries. He’s a Certified Google Ads Specialist and HubSpot CMS Developer who has personally managed budgets ranging from $500 to over $10,000/month.

Jake specializes in helping small businesses and solo marketers get more from lean ad budgets—using practical strategies that drive qualified leads, not just traffic. He shares real-world lessons on his blog, YouTube channel, and in his published books on digital marketing.



Topic:

Related posts

Tags: , ,

Want to Get Email Updates of New Articles?

Join My Email Newsletter