在WooCommerce产品搜索中启用自定义分类法 [英] Enable custom taxonomies in WooCommerce product search

查看:88
本文介绍了在WooCommerce产品搜索中启用自定义分类法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的:通过搜索产品的名称,描述和product_tag,修改WooCommerce搜索表单(在前端)的查询以显示产品。


我拥有的东西:我正在尝试使用此代码

解决方案

有很多错误和代码中的错误(例如,需要纳税查询)


要将 WooCommmerce产品标签字词包含在WooCommerce产品中仅限uct搜索 (前端)使用以下命令:

  add_filter('posts_search','woocommerce_search_product_tag_extended ',999,2); 
函数woocommerce_search_product_tag_extended($ search,$ query){
全局$ wpdb,$ wp;

$ qvars = $ wp-> query_vars;

if(is_admin()||空($ search)||!(isset($ qvars ['s'])
&& isset($ qvars ['post_type' ])&&!empty($ qvars ['s'])
&& $ qvars ['post_type'] ==='product')){
返回$ search;
}

//在这里设置您的自定义分类法
$ taxonomy =‘product_tag’; // WooCommerce产品标签

//获取产品ID
$ ids = get_posts(array(
'posts_per_page'=> -1,
'post_type' =>'product',
'post_status'=>'publish',
'fields'=>'ids,
'tax_query'=> array(array(
'分类法'=> $分类法,
'field'=>'名称',
'terms'=> esc_attr($ qvars ['s']),
)),
));

if(count($ ids)> 0){
$ search = str_replace('AND((((,,&AND(((({{$ wpdb-&posts} .ID IN(。implode(',',$ ids)。))OR(,$ search);
}
return $ search;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试并可以工作。


也要使其在WordPress搜索上起作用替换:

  if(is_admin()||空($ search)||!(isset($ qvars ['s'])
&& isset($ qvars ['post_type'])&&!空($ qvars ['s'])
&& $ qvars ['post_type'] ==='product')){

通过以下方式:

  if(is_admin()|| empty($ search)||!(isset( $ qvars ['s'])&&!empty($ qvars ['s]]))){





对于WooCommerce 产品类别,您将替换:

  $分类= product_tag; // WooCommerce产品标签

带有:

  $ taxonomy ='product_cat'; // WooCommerce产品类别




对于WooCommerce 产品品牌,您将替换为:

  $ taxonomy ='product_tag'; // WooCommerce产品标签

带有:

  $ taxonomy ='product_brand'; // WooCommerce产品品牌




用于多个分类法。


要同时搜索产品类别和产品标签术语,请使用:

  add_filter( 'posts_search','woocommerce_search_product_tag_extended',999,2); 
函数woocommerce_search_product_tag_extended($ search,$ query){
全局$ wpdb,$ wp;

$ qvars = $ wp-> query_vars;

if(is_admin()||空($ search)||!(isset($ qvars ['s'])
&& isset($ qvars ['post_type' ])&&!empty($ qvars ['s'])
&& $ qvars ['post_type'] ==='product')){
返回$ search;
}

//在此处将您的自定义分类法设置为数组
$ taxonomies = array(‘product_tag’,‘product_cat’);

$ tax_query = array(’relation’=>‘OR’); //初始化税务查询

//遍历分类法以将税务查询
设置为eacheach($ taxonomies as $ taxonomy){
$ tax_query [] = array(
'分类法'=> $分类法,
'field'=>'名称',
'terms'=> esc_attr($ qvars ['s']),
) ;
}

//获取产品ID
$ ids = get_posts(array(
'posts_per_page'=> -1,
'post_type' =>'product',
'post_status'=>'publish',
'fields'=>'ids',
'tax_query'=> $ tax_query,
));

if(sizeof($ ids)> 0){
$ search = str_replace('AND(((,,&AND(((({{$ wpdb-&posts} .ID IN(。implode(',',$ ids)。))OR(,$ search);
}
return $ search;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试并有效。


也要使其在WordPress搜索上起作用替换:

  if(is_admin()||空($ search)||!(isset($ qvars ['s'])
&& isset($ qvars ['post_type'])&&!空($ qvars ['s'])
&& $ qvars ['post_type'] ==='product')){

通过以下方式:

  if(is_admin()|| empty($ search)||!(isset( $ qvars ['s'])&&!empty($ qvars ['s]]))){


新线程将WooCommerce产品搜索扩展到自定义分类和自定义字段


What I want: modify the query of the WooCommerce search form (in frontend) to display the products by searching in the name, description and product_tag of the products.

What I have: I'm trying with this code inspired from this answer that return a result for the name and description of the product. But if i make a search with the tags names, no results. The search query don't search in tags of the product.

How to reproduce this issue (put the code below in functions.php file of your active theme):

function search_product_by_tag( $search, &$query_vars ) {
    global $wpdb, $pagenow;

    if ( 'edit.php' == $pagenow || empty($search) ) {
        return $search;
    }

    $args = array(
        'posts_per_page'  => -1,
        'post_type'       => 'product',
       'meta_query' => array(
            array(
                'key' => 'taxonomy',
                'value' => 'product_tag',
                'field' => 'name',
                'terms' => array($query_vars->query['s']),
                'compare' => 'LIKE',
    )));
    $posts = get_posts( $args );
    if ( empty( $posts ) ) return $search;
    $get_post_ids = array();
    foreach($posts as $post){
        $get_post_ids[] = $post->ID;
    }
    if ( sizeof( $get_post_ids ) > 0 ) {
        $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
    }
    return $search;
}
add_filter( 'posts_search', 'search_product_by_tag', 999, 2 );

Example: I've one product : Black Chocolate with the product tag "confection". With this code, if i search "Chocolate" in the search form, the product will be returned. But if i search "confection" : no results.

解决方案

There are a lot of mistakes and errors in your code (a tax query is required for example).

To include WooCommmerce product tag terms in WooCommerce product search only (front end) use the following:

add_filter( 'posts_search', 'woocommerce_search_product_tag_extended', 999, 2 );
function woocommerce_search_product_tag_extended( $search, $query ) {
    global $wpdb, $wp;

    $qvars = $wp->query_vars;

    if ( is_admin() || empty($search) ||  ! ( isset($qvars['s'])
    && isset($qvars['post_type']) && ! empty($qvars['s'])
    && $qvars['post_type'] === 'product' ) ) {
        return $search;
    }
    
    // Here set your custom taxonomy
    $taxonomy = 'product_tag'; // WooCommerce product tag

    // Get the product Ids
    $ids = get_posts( array(
        'posts_per_page'  => -1,
        'post_type'       => 'product',
        'post_status'     => 'publish',
        'fields'          => 'ids',
        'tax_query'       => array( array(
            'taxonomy' => $taxonomy,
            'field'    => 'name',
            'terms'    => esc_attr($qvars['s']),
        )),
    ));

    if ( count( $ids ) > 0 ) {
        $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $ids ) . ")) OR (", $search);
    }
    return $search;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

To make it work On WordPress search too replace:

    if ( is_admin() || empty($search) ||  ! ( isset($qvars['s'])
    && isset($qvars['post_type']) && ! empty($qvars['s'])
    && $qvars['post_type'] === 'product' ) ) {

By the following:

    if ( is_admin() || empty($search) ||  ! ( isset($qvars['s']) && ! empty($qvars['s']) ) ) {


For WooCommerce product category you will replace:

$taxonomy = 'product_tag'; // WooCommerce product tag

with:

$taxonomy = 'product_cat'; // WooCommerce product category


For WooCommerce product brands you will replace:

$taxonomy = 'product_tag'; // WooCommerce product tag

with:

$taxonomy = 'product_brand'; // WooCommerce product Brands


For multiple taxonomies.

To enable the search for both product category terms and product tag terms, you will use:

add_filter( 'posts_search', 'woocommerce_search_product_tag_extended', 999, 2 );
function woocommerce_search_product_tag_extended( $search, $query ) {
    global $wpdb, $wp;

    $qvars = $wp->query_vars;

    if ( is_admin() || empty($search) ||  ! ( isset($qvars['s'])
    && isset($qvars['post_type']) && ! empty($qvars['s'])
    && $qvars['post_type'] === 'product' ) ) {
        return $search;
    }

    // Here set your custom taxonomies in the array
    $taxonomies = array('product_tag', 'product_cat');

    $tax_query  = array('relation' => 'OR'); // Initializing tax query

    // Loop through taxonomies to set the tax query
    foreach( $taxonomies as $taxonomy ) {
        $tax_query[] = array(
            'taxonomy' => $taxonomy,
            'field'    => 'name',
            'terms'    => esc_attr($qvars['s']),
        );
    }

    // Get the product Ids
    $ids = get_posts( array(
        'posts_per_page'  => -1,
        'post_type'       => 'product',
        'post_status'     => 'publish',
        'fields'          => 'ids',
        'tax_query'       => $tax_query,
    ) );

    if ( sizeof( $ids ) > 0 ) {
        $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $ids ) . ")) OR (", $search);
    }
    return $search;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

To make it work On WordPress search too replace:

    if ( is_admin() || empty($search) ||  ! ( isset($qvars['s'])
    && isset($qvars['post_type']) && ! empty($qvars['s'])
    && $qvars['post_type'] === 'product' ) ) {

By the following:

    if ( is_admin() || empty($search) ||  ! ( isset($qvars['s']) && ! empty($qvars['s']) ) ) {

New Thread: Extend WooCommerce product search to custom taxonomies and custom fields

这篇关于在WooCommerce产品搜索中启用自定义分类法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆