【问题标题】:Enable custom taxonomies in WooCommerce product search在 WooCommerce 产品搜索中启用自定义分类
【发布时间】:2020-07-28 09:33:04
【问题描述】:

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

我有什么:我正在尝试使用此代码inspired from this answer,它返回产品名称和描述的结果。但是,如果我使用标签名称进行搜索,则没有结果。搜索查询不搜索产品的标签。

如何重现这个问题(将下面的代码放在你的活动主题的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 );

示例:我有一个产品:带有产品标签“糖果”的黑巧克力。使用此代码,如果我在搜索表单中搜索“巧克力”,产品将被返回。但如果我搜索“糖果”:没有结果。

【问题讨论】:

    标签: php wordpress search woocommerce custom-taxonomy


    【解决方案1】:

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

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

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

    代码在您的活动子主题(或活动主题)的functions.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;
    }
    

    代码在您的活动子主题(或活动主题)的functions.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']) ) ) {
    

    新话题:Extend WooCommerce product search to custom taxonomies and custom fields

    【讨论】:

    • 嗨@LoicTheAztec,感谢您的回复。但我不谈论管理产品过滤器,我想谈论前端(客户)搜索表单查询。我已经更新了我的问题,包括我想谈的内容的屏幕截图。
    • 感谢您的帮助。我已将第一个代码放在我的 function.php 文件中,但如您所见,查询不在 product_tag 中搜索:产品配置:imgur.com/1m2rHwQ 搜索结果:imgur.com/wbkFhwK
    • 这太不可思议了,因为在您的测试服务器中,我搜索“hat”并且我有 2 个结果:2 个页面的描述包含“what”。 cbleu.net/sites/woo/?s=hat 但没有一款产品带有“帽子”标签。那你呢 ? i.gyazo.com/795f10f89258e753d46c71411d441b01.png
    • 嗨@LoicTheAztec,我也想在搜索中添加产品sku、产品标题和产品简短描述,请帮助
    • 嗨@LoicTheAztec,对不起,我是新来的,还在学习 StackOverflow 的功能。我只是赞成答案 :) 关于这个问题,我已经实现了 product_tag 和 product_category 搜索,最后我只想要搜索查询中的 sku。这是我的问题:stackoverflow.com/questions/63541202/…
    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2020-12-11
    • 2019-11-12
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多