我需要wc_get_products()按标签选择产品,但只选择指定了所有标签的产品。
示例:
产品A有产品标签TagX和TagY__。
产品B有产品标签TagX__。
如果我设置:
$args = array(
'tag' => array('TagX', 'TagY')
);wc_get_products()返回Product 和Product ,但我希望它在本例中只返回Product 。
有什么解决办法吗?
发布于 2019-11-19 07:36:54
请试一试--我还没有测试过它,但是我相信你应该知道我想传达的信息,
$args = array(
'tag' => array('TagX', 'TagY'),
'return' => 'objects'
);
$list_of_woocommerce_products = wc_get_products($args);
$required_array_of_objects = array();
$tag = get_term_by('slug', 'TagX');
$tag_id_X = $tag->term_id;
$tag = get_term_by('slug', 'TagY');
$tag_id_Y = $tag->term_id;
foreach($list_of_woocommerce_products as $single_woocommerce_product){
if( in_array($tag_id_X, $single_woocommerce_product->get_tag_ids()) && in_array($tag_id_X, $single_woocommerce_product->get_tag_ids()) ){
array_push($required_array_of_objects,$single_woocommerce_product);
}
}
//Required array of Woocommerce products having tags X and Y
print_r($required_array_of_objects);请研究和分析上述untested-code.在得到基本想法后做出适当的修改,并将结果告诉我。
https://stackoverflow.com/questions/58928476
复制相似问题