在 WooCommerce 中获取处理状态订单计数?
Get processing status orders count in WooCommerce?
我想在 WooCommerce 中获取处理订单数。我在 Code Snippet 插件中使用了以下代码,但它正在运行。
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();
它没有返回任何内容。
这个方法可以帮到你。订单存储为 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);
}
此自定义函数使用非常简单的 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 文件。已测试并有效。
用法示例 for "processing" 订单数:
// 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,
)));
看看:https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
在类似的情况下,该参考确实帮助了我。
与使用硬编码查询或其他答案中提到的 WP_Query 相比,使用 WC 函数可确保您的代码更具前瞻性。
我想在 WooCommerce 中获取处理订单数。我在 Code Snippet 插件中使用了以下代码,但它正在运行。
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();
它没有返回任何内容。
这个方法可以帮到你。订单存储为 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);
}
此自定义函数使用非常简单的 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 文件。已测试并有效。
用法示例 for "processing" 订单数:
// 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,
)));
看看:https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query 在类似的情况下,该参考确实帮助了我。 与使用硬编码查询或其他答案中提到的 WP_Query 相比,使用 WC 函数可确保您的代码更具前瞻性。