Skip to content
Advertisement

Extend WooCommerce product search to custom taxonomies and custom fields

I am creating advance woocommerce search and I want to add sku and product_tag and product_category in search query. Below I am using Enable custom taxonomies in WooCommerce product search answer code, that enable search for multiple taxonomies:

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

I want to add product sku too in search query, how to add it ?

Advertisement

Answer

The following will extend Product search to multiple taxonomies (Product Category and Product tag) and multiple custom fields (as SKU here):

add_filter( 'posts_search', 'woocommerce_search_product_mega_extended', 999, 2 );
function woocommerce_search_product_mega_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;
    }

    // SETTINGS:
    $taxonomies = array('product_tag', 'product_cat'); // Here set your custom taxonomies in the array
    $meta_keys  = array('_sku'); // Here set your product meta key(s) in the array

    // Initializing tax query
    $tax_query  = count($taxonomies) > 1 ? array('relation' => 'OR') : array();

    // 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 from taxonomy(ies)
    $tax_query_ids = (array) get_posts( array(
        'posts_per_page'  => -1,
        'post_type'       => 'product',
        'post_status'     => 'publish',
        'fields'          => 'ids',
        'tax_query'       => $tax_query,
    ) );

    // Initializing meta query
    $meta_query = count($meta_keys) > 1 ? array('relation' => 'OR') : array();

    // Loop through taxonomies to set the tax query
    foreach( $taxonomies as $taxonomy ) {
        $meta_query[] = array(
            'key'     => '_sku',
            'value'   => esc_attr($qvars['s']),
        );
    }

    // Get the product Ids from custom field(s)
    $meta_query_ids = (array) get_posts( array(
        'posts_per_page'  => -1,
        'post_type'       => 'product',
        'post_status'     => 'publish',
        'fields'          => 'ids',
        'meta_query'      => $meta_query,
    ) );

    $product_ids = array_unique( array_merge( $tax_query_ids, $meta_query_ids ) ); // Merge Ids in one array  with unique Ids

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

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

Related: Enable custom taxonomies in WooCommerce product search

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement