4

我想问一下如何创建 woocommerce 自定义搜索表单。

我的搜索和过滤表单有 3 个字段:

类别:我设法将 woocommerce 产品类别拉入带有<select>标签的自定义搜索 html 表单中:

<?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; ?>

按作者筛选:(下拉菜单):

woo_add_custom_general_fields_save()我使用主题上的功能正常工作,为 woocommerce 添加了额外的字段functions.php,

注意:这不是我们通常在 wordpress 上用来添加更多元数据的“自定义字段”,但下面的代码是在 Product Data > General(在 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 ) );
}

按标题:

我设法使用这个过滤器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_cattag_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 搜索?

谢谢你。

4

1 回答 1

1

在您的“pm-bookstore”页面中,使用 WP_Query 获取结果。

// WP_Query arguments
$args = array (
    'name'                   => 'your title',
    'post_type'              => array( 'product' ),
    'post_status'            => array( 'publish' ),
    'tax_query'              => array(
             'taxonomy' => 'categories',
             'field'    => 'slug',
             'term'     =>  'your category slug'
     ),
    'meta_query'             => array(
        array(
            'key'       => '_book_author',
            'value'     => 'your author name',
            'compare'   => '=',
            'type'      => 'CHAR',
        ),
    ),
);

// The Query
$query = new WP_Query( $args );

我没有测试过它,但它应该可以工作。

于 2016-08-02T13:32:02.643 回答