【问题标题】:Woocommerce Filter Product by AttributeWoocommerce 按属性过滤产品
【发布时间】:2015-06-02 02:42:40
【问题描述】:

我一直在搜索许多博客和论坛,但似乎还没有答案。所以我试图找到一种方法如何按属性过滤产品。我正在使用 ajax 来提取数据并附加它。问题是根据什么属性进行循环的查询是什么。

示例。
产品 A 有颜色:蓝色、红色、绿色,并有品牌:BrandA 、BrandB
产品 B 有颜色:粉红色、红色、黑色。

我想要的只是在不使用任何插件的情况下在单个查询中获取具有颜色 [红色和绿色] 和品牌 [BrandA] 属性的所有产品。 这是我的functions.php中的代码

function advanced_search(){

     $html = "";
     $args = array( 'post_type' => 'product','product_cat' => 'yarn');
     $loop = new WP_Query( $args );
     while ( $loop->have_posts() ){
         $loop->the_post();
         ob_start();
         get_template_part( 'templates/part', 'search-result' ); 
         $html = ob_get_contents();
         ob_end_clean();
     }  
     wp_send_json(array(  "product_id" => $product_id , "html"  => $html ));
}
add_action('wp_ajax_get_advanced_search', 'advanced_search');
add_action('wp_ajax_nopriv_get_advanced_search', 'advanced_search'); 

我不知道我应该将产品属性放在我的查询中的哪个位置。我希望有人能找到答案。非常感谢。

【问题讨论】:

    标签: filter attributes woocommerce product


    【解决方案1】:

    您应该能够通过将tax_queryWP_Query 结合使用来实现您想要做的事情。附件是一个使用“品牌”属性和其中的“EVGA”术语的小示例(WooCommerce 将转换为“pa_brand” - 产品属性品牌)。

    $query = new WP_Query( array(
        'tax_query' => array(
            'relation'=>'AND',
            array(
                'taxonomy' => 'pa_brand',
                'field' => 'slug',
                'terms' => 'evga'
            )
        )
    ) );
    

    您可以在上述链接中找到更多文档,将一些税务查询串在一起,以获得额外的过滤和功能。

    【讨论】:

    • 太棒了!它现在工作。我把我的条款放在条款中。我应该在“字段”中输入什么值?它现在可以正常工作,而无需从“slug”值更改它。对不起,我不能投票给你。我没有足够的声誉。顺便谢谢你。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2020-08-30
    • 2021-03-30
    • 2016-02-20
    • 1970-01-01
    • 2019-02-12
    相关资源
    最近更新 更多