
7折
减价出售
¥799
wc_get_orders()
是WooCommerce提供的强大订单查询函数,其核心参数包括:
$orders = wc_get_orders([
'status' => 'completed', // 订单状态
'limit' => 10, // 返回数量限制
'paginate' => true, // 是否分页
'return' => 'objects', // 返回类型
'orderby' => 'date', // 排序字段
'order' => 'DESC', // 排序方式
'date_created' => '2023-01-01...2023-12-31' // 日期范围
]);
WooCommerce订单状态系统:
$statuses = [
'pending' => '待付款',
'processing' => '处理中',
'on-hold' => '等待中',
'completed' => '已完成',
'cancelled' => '已取消',
'refunded' => '已退款',
'failed' => '失败'
];
$args = [
'type' => 'shop_order',
'limit' => -1,
'return' => 'ids',
'meta_query' => [
[
'key' => '_billing_country',
'value' => 'US',
'compare' => '='
],
[
'key' => '_order_total',
'value' => 100,
'compare' => '>'
]
],
'date_query' => [
[
'after' => 'January 1, 2023',
'before' => 'December 31, 2023',
'inclusive' => true
]
]
];
$order_ids = wc_get_orders($args);
// 查询购买了特定产品的订单
$product_id = 123;
$args = [
'status' => 'completed',
'limit' => -1,
'meta_query' => [
[
'key' => '_product_id',
'value' => $product_id,
'compare' => '='
]
]
];
$product_orders = wc_get_orders($args);
// 查询特定客户的订单历史
$customer_id = 456;
$customer_orders = wc_get_orders([
'customer_id' => $customer_id,
'return' => 'objects'
]);
// 轻量级查询只获取ID
$order_ids = wc_get_orders([
'status' => 'completed',
'limit' => -1,
'return' => 'ids' // 仅获取ID减少内存占用
]);
// 分批处理大数据量
$batch_size = 100;
$page = 1;
$all_orders = [];
do {
$orders = wc_get_orders([
'limit' => $batch_size,
'paginate' => true,
'paged' => $page
]);
$all_orders = array_merge($all_orders, $orders->orders);
$page++;
} while ($page <= $orders->max_num_pages);
function get_cached_orders($args) {
$cache_key = 'wc_orders_' . md5(serialize($args));
$orders = get_transient($cache_key);
if (false === $orders) {
$orders = wc_get_orders($args);
set_transient($cache_key, $orders, HOUR_IN_SECONDS);
}
return $orders;
}
// 使用示例
$recent_orders = get_cached_orders([
'status' => 'completed',
'date_created' => 'last month',
'limit' => 50
]);
function generate_sales_report($start_date, $end_date) {
$orders = wc_get_orders([
'status' => 'completed',
'limit' => -1,
'date_created' => $start_date . '...' . $end_date,
'return' => 'objects'
]);
$report = [
'total_sales' => 0,
'order_count' => count($orders),
'products_sold' => 0,
'customers' => []
];
foreach ($orders as $order) {
$report['total_sales'] += $order->get_total();
foreach ($order->get_items() as $item) {
$report['products_sold'] += $item->get_quantity();
}
$customer_id = $order->get_customer_id();
if ($customer_id && !in_array($customer_id, $report['customers'])) {
$report['customers'][] = $customer_id;
}
}
$report['average_order'] = $report['order_count'] > 0
? $report['total_sales'] / $report['order_count']
: 0;
return $report;
}
function analyze_customer_behavior($customer_id) {
$orders = wc_get_orders([
'customer_id' => $customer_id,
'status' => ['completed', 'processing'],
'return' => 'objects'
]);
$analysis = [
'total_spent' => 0,
'order_count' => count($orders),
'favorite_categories' => [],
'purchase_frequency' => 0
];
$first_order_date = null;
$last_order_date = null;
$category_counts = [];
foreach ($orders as $order) {
$analysis['total_spent'] += $order->get_total();
$order_date = $order->get_date_created();
if (!$first_order_date || $order_date < $first_order_date) {
$first_order_date = $order_date;
}
if (!$last_order_date || $order_date > $last_order_date) {
$last_order_date = $order_date;
}
foreach ($order->get_items() as $item) {
$product = $item->get_product();
if ($product) {
$categories = $product->get_category_ids();
foreach ($categories as $cat_id) {
$category_counts[$cat_id] = ($category_counts[$cat_id] ?? 0) + $item->get_quantity();
}
}
}
}
// 计算购买频率
if ($first_order_date && $last_order_date) {
$days = $first_order_date->diff($last_order_date)->days;
$analysis['purchase_frequency'] = $days > 0
? $analysis['order_count'] / ($days / 30) // 每月平均订单数
: $analysis['order_count'];
}
// 找出最常购买的前3个分类
arsort($category_counts);
$analysis['favorite_categories'] = array_slice(array_keys($category_counts), 0, 3);
return $analysis;
}
class Advanced_Order_Query {
public static function get_orders_by_product($product_id, $args = []) {
$defaults = [
'status' => 'completed',
'limit' => -1,
'return' => 'objects'
];
$args = wp_parse_args($args, $defaults);
global $wpdb;
$order_ids = $wpdb->get_col($wpdb->prepare(
"SELECT DISTINCT order_id
FROM {$wpdb->prefix}woocommerce_order_items oi
JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim ON oi.order_item_id = oim.order_item_id
WHERE oim.meta_key = '_product_id' AND oim.meta_value = %d",
$product_id
));
if (empty($order_ids)) {
return [];
}
$args['post__in'] = $order_ids;
return wc_get_orders($args);
}
public static function get_high_value_orders($threshold = 1000, $args = []) {
$args['meta_query'] = [
[
'key' => '_order_total',
'value' => $threshold,
'compare' => '>=',
'type' => 'NUMERIC'
]
];
return wc_get_orders($args);
}
}
// 使用示例
$big_orders = Advanced_Order_Query::get_high_value_orders(5000, [
'date_created' => 'last year'
]);
add_action('rest_api_init', function() {
register_rest_route('wc/v3', '/orders/recent', [
'methods' => 'GET',
'callback' => 'get_recent_orders_api',
'permission_callback' => function() {
return current_user_can('edit_shop_orders');
}
]);
});
function get_recent_orders_api($request) {
$days = $request->get_param('days') ?: 7;
$limit = $request->get_param('limit') ?: 10;
$orders = wc_get_orders([
'limit' => $limit,
'date_created' => '>' . (time() - DAY_IN_SECONDS * $days),
'return' => 'objects'
]);
$data = [];
foreach ($orders as $order) {
$data[] = [
'id' => $order->get_id(),
'date' => $order->get_date_created()->format('Y-m-d H:i:s'),
'status' => $order->get_status(),
'total' => $order->get_total(),
'customer' => [
'id' => $order->get_customer_id(),
'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name()
]
];
}
return rest_ensure_response($data);
}
// 使用分页处理大量订单
function process_large_order_set($callback, $batch_size = 100) {
$page = 1;
do {
$orders = wc_get_orders([
'limit' => $batch_size,
'paginate' => true,
'paged' => $page,
'return' => 'objects'
]);
foreach ($orders->orders as $order) {
call_user_func($callback, $order);
}
$page++;
} while ($page <= $orders->max_num_pages);
}
// 使用示例:更新所有订单的元数据
process_large_order_set(function($order) {
if (!$order->meta_exists('_processed_v2')) {
// 执行处理逻辑
$order->update_meta_data('_processed_v2', 'yes');
$order->save();
}
});
// 查询自定义订单状态
function get_orders_with_custom_status($status) {
$valid_statuses = wc_get_order_statuses();
if (!isset($valid_statuses['wc-' . $status])) {
return new WP_Error('invalid_status', '无效的订单状态');
}
return wc_get_orders([
'status' => $status,
'limit' => -1
]);
}
// 查询多个状态组合
function get_orders_multiple_statuses($statuses) {
return wc_get_orders([
'status' => $statuses,
'limit' => -1
]);
}
// 使用示例
$active_orders = get_orders_multiple_statuses(['processing', 'on-hold']);
通过深入掌握wc_get_orders
函数,开发者可以构建各种复杂的订单查询和处理逻辑,从简单的订单检索到复杂的业务分析报表。这个强大的函数是WooCommerce订单管理的核心工具,合理使用可以显著提升电商系统的开发效率和运行性能。
减价出售
减价出售
减价出售
减价出售
电话咨询
1855-626-3292
微信咨询