Rom*_*usa 1 attributes product filter woocommerce
我一直在寻找许多博客和论坛,但这似乎还没有回答.所以我试图找到一种如何按属性过滤产品的方法.我正在使用ajax来提取数据并附加它.问题是,使循环的查询取决于什么属性.
例.
产品A有颜色:蓝色,红色,绿色,品牌有:BrandA,BrandB
产品B有颜色:粉红色,红色,黑色.
我想要的是在不使用任何插件的情况下,在单个查询中获得具有Color [红色和绿色]属性和Brand [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');
Run Code Online (Sandbox Code Playgroud)
我不知道我应该在哪里将产品属性放在我的查询中.我希望有人能找到答案.非常感谢你.
通过将tax_query与WP_Query结合使用,您应该能够实现您想要做的事情.附件是一个使用"品牌"属性和其中的"EVGA"术语的小例子(WooCommerce将变成'pa_brand' - 产品属性品牌).
$query = new WP_Query( array(
'tax_query' => array(
'relation'=>'AND',
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => 'evga'
)
)
) );
Run Code Online (Sandbox Code Playgroud)
您可以在上面的链接中找到更多文档,将一些税务查询串起来,以获得额外的过滤和功能.