3

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. Frontend search result

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275

1 Answers1

4

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

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
  • Hi @LoicTheAztec and thanks for the reply. But i don't talk about the admin product filter, i wanna talk about the frontend (customer) search form query. I've update my question to include a screenshot of what i wanna talk. – Benoit FOUCHER Jul 28 '20 at 17:39
  • Thanks you for your help. I've put the first code in my function.php file but, as you can see, the query don't search in product_tag : Product configuration : https://imgur.com/1m2rHwQ Search result : https://imgur.com/wbkFhwK – Benoit FOUCHER Jul 29 '20 at 18:59
  • That is incredible because, in your test server, i search "hat" and i have 2 results : 2 pages whose the description contain "what". https://www.cbleu.net/sites/woo/?s=hat **But no one product with the tag "hat"**. What about you ? https://i.gyazo.com/795f10f89258e753d46c71411d441b01.png – Benoit FOUCHER Jul 29 '20 at 20:00
  • Hi @LoicTheAztec, I want to add product sku and product title and product short description in the search too, Please help – Hannah James Aug 22 '20 at 20:43
  • Hi @LoicTheAztec, pardon me, I am new here and still learning StackOverflow features. I just upvote the answer :) Regarding the question, I have achieved product_tag and product_category search, and last I only want sku in search query. Here is my question: https://stackoverflow.com/questions/63541202/modify-woocommerce-default-search-with-sku-and-product-tag-and-product-category – Hannah James Aug 22 '20 at 21:37
  • @LoicTheAztec, Sure sir, Thank you :) – Hannah James Aug 22 '20 at 22:06
  • @HannahJames Answered. – LoicTheAztec Aug 22 '20 at 22:15
  • @LoicTheAztec Its perfectly working sir :) Thank you so much once again – Hannah James Aug 22 '20 at 22:20