将 WooCommerce 产品搜索扩展到自定义分类法和自定义字段 [英] Extend WooCommerce product search to custom taxonomies and custom fields

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

问题描述

我正在创建高级 woocommerce 搜索,我想在搜索查询中添加 sku 和 product_tag 以及 product_category.下面我使用 在 WooCommerce 产品搜索中启用自定义分类法 答案代码,可以搜索多个分类法:

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

我也想在搜索查询中添加产品sku,如何添加?

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

推荐答案

以下将产品搜索扩展到多个分类法(产品类别和产品标签)和多个自定义字段(如此处的 SKU):

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

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

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

相关:在 WooCommerce 产品搜索中启用自定义分类法

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

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