大家好,我正在尝试获得创建功能以获取产品的最小值& WooCommerce查询中的最高价格。 问题是我的查询没有获得价格的最低产品
产品的示例:
- 200 USD
- 300 USD出售
- 400 USD
- 500 USD
- 600 USD
,其结果是具有400美元的产品,必须是200不是400。
我的查询:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 1,
'ignore_stickie_posts' => true,
'fields' => 'ids',
'orderby' => 'price',
'order' => 'DESC',
);
$products_price_min_args = get_posts($args);
$string_id = implode(",", $products_price_min_args);
$product = wc_get_product( $string_id );
if($product) {
if ( $product->is_type('variable') ){
$price_min = $product->get_variation_price('min'); // Float number
}
elseif ( $product->is_type('simple') ){
if( $product->is_on_sale() ) {
$price_min = $product->get_sale_price();
}
else{
$price_min = $product->get_price();
}
}
return $price_min;
}
hello guys I'm trying to get create a function to get the product has the most min & max Price in the woocommerce query.
problem is my query not get the lowest product that has price
here example of products:
- 200 USD
- 300 USD ON SALE
- 400 USD
- 500 USD
- 600 USD
and the result is the product with 400 USD, and it must be 200 not 400.
my query :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 1,
'ignore_stickie_posts' => true,
'fields' => 'ids',
'orderby' => 'price',
'order' => 'DESC',
);
$products_price_min_args = get_posts($args);
$string_id = implode(",", $products_price_min_args);
$product = wc_get_product( $string_id );
if($product) {
if ( $product->is_type('variable') ){
$price_min = $product->get_variation_price('min'); // Float number
}
elseif ( $product->is_type('simple') ){
if( $product->is_on_sale() ) {
$price_min = $product->get_sale_price();
}
else{
$price_min = $product->get_price();
}
}
return $price_min;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WooCommerce产品价格存储在此表中-WP_WC_PRODUCT_META_LOOKUP,
因此,您必须从该表中查询数据并获取最小价格。当您使用get_posts时,它正在从默认的WordPress邮局表中获取产品,该表未存储产品的价格。
这是从帖子表中获取产品最小价格的更新代码 -
The Woocommerce product prices are stored in this table - wp_wc_product_meta_lookup
So, you have to query the data from this table and get the min price. As you are using get_posts so it is getting the product from the default WordPress posts table where the price for the product isn't stored.
Here is the updated code to fetch the min price of the product from the posts meta table -