dongzao4503 2014-04-28 10:52
浏览 158

WooCommerce按类别和自定义字段过滤搜索

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:

http://www.example.com/wp-root/woocommerce-product-page/?s=searchproducttitle&category=categoryslug&author=authorname

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.

  • 写回答

1条回答 默认 最新

  • dongyan5239 2016-08-02 13:32
    关注

    In your 'pm-bookstore' page use WP_Query to get the result.

    // 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 );
    

    I haven't tested it but it should work.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数