【发布时间】: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