提问者:小点点

在WooCommerce产品搜索中启用自定义分类


我想要的:修改WooCommerce搜索表单的查询(在前端),通过搜索产品的名称、说明和产品标签来显示产品。

我所拥有的:我正在尝试从这个答案中获得灵感的代码,该答案返回产品名称和描述的结果。但是如果我用标签名称搜索,没有结果。搜索查询不搜索产品的标签。

如何重现这个问题(把下面的代码放在functions.php活动主题的文件中):

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


共1个答案

匿名用户

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

要仅在WooCommerce产品搜索(前端)中包含WooCommerce产品标签术语,请使用以下内容:

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

代码进入函数。活动子主题(或活动主题)的php文件。测试和工作。

要使其在WordPress搜索上工作,请替换:

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

通过以下方式:

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

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

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

与:

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

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

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

与:

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

要启用产品类别术语和产品标签术语的搜索,您将使用:

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

代码进入函数。活动子主题(或活动主题)的php文件。测试和工作。

要使其在WordPress搜索上工作,请替换:

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

通过以下方式:

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

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