WooCommerce: Custom search form that shows product attributes and queries them

Viewed 14

I want to add a custom search form incl. an option to choose certain product attributes. The goal is that users can input a text and choose a product attribute value and results are only shown from a certain product category.

I know that I can utilize the type parameter of input to build a custom URL. How can I include the terms of a attributes as well?

By using this answer (https://wordpress.stackexchange.com/questions/184166/create-product-category-and-keyword-search-form-in-woocommerce) my code looks like this so far:

function custom_search( $form ) {

$terms = get_terms('pa_attributes');

$form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/'  ) ) . '">
    <div>
        <label class="screen-reader-text" for="s">' . __( 'Search for:', 'woocommerce' ) . '</label>
        <input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . __( 'Search within a category', 'woocommerce' ) . '" />
        <input type="hidden" name="post_type" value="product">
        <input type="hidden" name="taxonomy" value="product_cat">

        <!-- here: my_product_category is chosen -->
        <input type="hidden" name="term" value="my_product_category">

 <!-- HOW TO INCLUDE $terms ? -->


        <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search', 'woocommerce' ) .'" />
        <input type="hidden" name="post_type" value="product" />
    </div>
</form>';

return $form;
}
0 Answers
Related