如何获取特定类别的产品数量 "on-sale"?
How can I get the number of products "on-sale" for a specific category?
我是 woocommerce 2.8,我如何计算商店 "on sale" 中特定类别(例如:计算机)的现有产品?
我找到了计算所有产品的方法:
$count_posts = wp_count_posts( 'product' );
return $count_posts->publish;
但是我如何才能获得特定类别的产品数量 "on-sale"?
谢谢
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => here goes your category id, you can also go for slug
)
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$query = new WP_Query($args);
$onSaleCount = $query->found_posts;
以上查找属于 tax_query
下提到的类别的简单且可变的销售产品。
我不确定是否有更好的方法,但这应该可行。
LE - 更改参数以根据评论排除缺货产品。
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => here goes your category id, you can also go for slug
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
),
array(
'key' => '_stock_status',
'value' => 'instock'
),
)
);
我是 woocommerce 2.8,我如何计算商店 "on sale" 中特定类别(例如:计算机)的现有产品?
我找到了计算所有产品的方法:
$count_posts = wp_count_posts( 'product' );
return $count_posts->publish;
但是我如何才能获得特定类别的产品数量 "on-sale"?
谢谢
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => here goes your category id, you can also go for slug
)
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$query = new WP_Query($args);
$onSaleCount = $query->found_posts;
以上查找属于 tax_query
下提到的类别的简单且可变的销售产品。
我不确定是否有更好的方法,但这应该可行。
LE - 更改参数以根据评论排除缺货产品。
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => here goes your category id, you can also go for slug
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
),
array(
'key' => '_stock_status',
'value' => 'instock'
),
)
);