I want to ask how to create a custom woocommerce search form.
My search and filter form has 3 fields:
Category: I managed to pull the woocommerce product category into my custom html search form with the tag <select>:
<?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: (drop-down menu) by author:
I added an extra field to woocommerce, using a function woo_add_custom_general_fields_save()on a theme functions.php,that works correctly.
Note: this is not a “custom field” that we usually use in wordpress to add some more metadata, but this code below is to add more fields in Data 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 name:
I managed to use this filter using http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle
:
.
, html:
<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>
, . ? S ( ).
, product_cat, tag_ID, .
http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle
:
http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle&category=categoryslug&author=authorname
http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle&category=categoryslug
woocommerce?
.