【问题标题】:WooCommerce filter search by category and custom fieldWooCommerce 按类别和自定义字段过滤搜索
【发布时间】:2014-06-13 21:16:45
【问题描述】:

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

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

类别:我设法使用<select>标签将woocommerce产品类别拉入我的自定义搜索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; ?>

过滤依据:(下拉菜单)按作者:

我在主题的functions.php, 上使用woo_add_custom_general_fields_save() 函数向woocommerce 添加了额外的字段,可以正常工作。

注意:这不是我们通常在 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_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 搜索中起作用?

谢谢。

【问题讨论】:

  • 你有解决这个问题的办法吗?

标签: php wordpress woocommerce hook-woocommerce


【解决方案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 );

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

【讨论】:

    猜你喜欢
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2015-07-14
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多