我想获取 WooCommerce 中的处理订单计数。我在代码片段插件中使用以下代码,但它有效。
if( !function_exists( 'wc_processing_order_count' ) ) {
require_once '../plugins/woocommerce/includes/wc-order-functions.php';
}
// NOTICE! Understand what this does before running.
$result = wc_processing_order_count();
它什么也没返回。
此自定义函数使用非常简单的 SQL 查询来获取特定状态的订单计数:
function get_orders_count_from_status( $status ){
global $wpdb;
// We add 'wc-' prefix when is missing from order staus
$status = 'wc-' . str_replace('wc-', '', $status);
return $wpdb->get_var("
SELECT count(ID) FROM {$wpdb->prefix}posts WHERE post_status LIKE '$status' AND `post_type` LIKE 'shop_order'
");
}
代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并有效。
“处理”订单计数的使用示例:
// Display "processing" orders count
echo get_orders_count_from_status( "processing" );
对于如此简单的事情来说,上面的两个答案都太复杂了。
最简单的方法是这样的:)
$processing_orders_count = count(wc_get_orders( array(
'status' => 'processing',
'return' => 'ids',
'limit' => -1,
)));
这个方法可以帮助你。订单存储为 post_type shop_order。因此,通过创建查询来获取 shop_order 类型的所有帖子并传递参数来获取所有处理订单,您将能够获取这些订单
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('processing')
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ){
$loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
}
您可以简单地使用wc_processing_order_count()
$int = wc_processing_order_count();
echo $int;
https://woocommerce.wp-a2z.org/oik_api/wc_processing_order_count/
看一下:https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query 该参考资料在类似情况下确实对我有帮助。 与其他答案中提到的使用硬编码查询或 WP_Query 相比,使用 WC 函数可确保您的代码更具前瞻性。