I want to ask how to create a woocommerce custom search form.
My search and filter form has 3 fields:
Category: I managed to pull woocommerce product category into my custom search html form with <select>
tag:
<?php
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77'));
foreach($catTerms as $catTerm) : ?>
<option value="<?php echo $catTerm->slug; ?>"><?php echo $catTerm->name; ?></option>
<?php endforeach; ?>
Filter By: (dropdown menu) by author:
I added extra field to woocommerce using woo_add_custom_general_fields_save()
function on theme’s functions.php,
works properly.
Note: this is not ‘custom field’ we usually use on wordpress to add some more metadata, but this code below is to add more field on Product Data > General (on woocommerce).
function woo_add_custom_general_fields_save($post_id)
{
$woocommerce_textinput = $_POST['_book_author'];
if( !empty( $woocommerce_textinput ) )
update_post_meta( $post_id, '_book_author', esc_html( $woocommerce_textinput ) );
}
By Title:
I managed to use this filter using http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle
Input text field:
This is for user to search by keyword.
So this is my complete custom html search form:
<form action="<?php echo site_url(); ?>/pm-bookstore/" method="GET">
<select name="">
<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77')); ?>
<?php foreach($catTerms as $catTerm) : ?>
<option value="<?php echo $catTerm->slug; ?>"><?php echo $catTerm->name; ?></option>
<?php endforeach; ?>
</select>
<select name="">
<option value="">By author</option>
<option value="">By title</option>
</select>
<input type="text" placeholder="Search Book by Title, Author, ISBN..." name="s">
<button class="fa fa-search" type="submit"></button>
</form>
For search parameter I expect to able to pull them all. But I only able to use the ?s parameter (which is only product title).
I tried using another parameter, like, product_cat
, tag_ID
, but no success.
At the moment I only able to use
http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle
My expected result is:
or
http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle&category=categoryslug
How to make this search parameter working on woocommerce search?
Thank you.
In your ‘pm-bookstore’ page use WP_Query to get the result.
I haven’t tested it but it should work.