如何在WooCommerce中通过商品ID获取订单详情(代码求助)
嘿,我来帮你搞定这两个WooCommerce相关的问题!
问题1:如何在WooCommerce中通过商品ID获取订单详情?
要获取包含指定商品ID的所有订单详情,核心思路是先筛选出包含该商品的订单,再逐个提取订单的关键信息。这里提供两种常用的实现方式:
方式一:用
WP_Query精准筛选(高效推荐)
直接通过订单的元数据和商品关联关系筛选,比先拉取所有订单再遍历更高效,适合订单量较大的场景:function get_orders_by_product_id( $product_id ) { $orders_query = new WP_Query( array( 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_order_statuses() ), 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => '_customer_user', 'value' => get_current_user_id(), // 仅查询当前用户订单,可按需移除 'compare' => '=', ), ), 'tax_query' => array( array( 'taxonomy' => 'product_id', 'field' => 'term_id', 'terms' => $product_id, ), ), ) ); $orders_details = array(); if ( $orders_query->have_posts() ) { while ( $orders_query->have_posts() ) { $orders_query->the_post(); $order = wc_get_order( get_the_ID() ); // 按需收集订单信息,可自由扩展字段 $orders_details[] = array( 'order_id' => $order->get_id(), 'order_status' => wc_get_order_status_name( $order->get_status() ), 'order_total' => wc_price( $order->get_total() ), 'order_date' => $order->get_date_created()->format('Y-m-d H:i:s'), ); } wp_reset_postdata(); } return $orders_details; } // 使用示例 $target_product_id = 123; $user_orders = get_orders_by_product_id( $target_product_id );方式二:遍历用户订单筛选商品(灵活可控)
如果需要对订单项做更细致的判断(比如检查商品变体),可以先拉取用户所有订单,再逐个检查订单项:function get_orders_containing_product( $product_id ) { $customer_orders = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => get_current_user_id(), 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_order_statuses() ), ) ); $matching_orders = array(); foreach ( $customer_orders as $order_post ) { $order = wc_get_order( $order_post->ID ); $items = $order->get_items(); foreach ( $items as $item ) { // 获取订单项对应的商品ID(变体商品返回父商品ID) $item_product_id = $item->get_product_id(); if ( $item_product_id == $product_id ) { $matching_orders[] = array( 'order_id' => $order->get_id(), 'product_name' => $item->get_name(), 'quantity' => $item->get_quantity(), 'order_total' => wc_price( $order->get_total() ), ); break; // 找到商品后跳出循环,避免重复添加同一订单 } } } return $matching_orders; }
问题2:修复你的代码问题
看了你的代码片段,我发现几个关键问题:
- 代码未完成:
foreach ( $ite...这里变量名写错了,应该是$items - 缺少商品ID筛选逻辑:你的代码只是获取当前用户的所有订单,但没有检查订单是否包含指定商品
- 潜在性能问题:直接拉取所有订单遍历,用户订单多的时候效率偏低
下面是修正并完善后的代码:
function getProductOrders($product_id){ $customer_orders = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => get_current_user_id(), 'post_type' => wc_get_order_types(), 'post_status' => array_keys( wc_get_order_statuses() ), ) ); $orders_with_product = array(); foreach ($customer_orders as $customer_order) { $order = wc_get_order( $customer_order->ID ); $items = $order->get_items(); foreach ( $items as $item ) { $item_product_id = $item->get_product_id(); // 匹配到目标商品后,记录订单详情 if ( $item_product_id == $product_id ) { $orders_with_product[] = array( 'order_id' => $order->get_id(), 'order_status' => wc_get_order_status_name( $order->get_status() ), 'order_date' => $order->get_date_created()->format('Y-m-d'), 'product_name' => $item->get_name(), 'product_quantity' => $item->get_quantity(), 'order_total' => wc_price( $order->get_total() ) ); break; } } } return $orders_with_product; } // 使用示例 $target_product_id = 456; // 替换为你的目标商品ID $user_product_orders = getProductOrders($target_product_id); // 打印结果查看 if ( !empty($user_product_orders) ) { foreach ($user_product_orders as $order_info) { echo "订单ID: {$order_info['order_id']} | 状态: {$order_info['order_status']} | 总价: {$order_info['order_total']}<br>"; } } else { echo "当前用户没有包含该商品的订单"; }
额外提示
- 如果需要支持商品变体ID查询,可以改用
$item->get_variation_id(),或者同时检查商品ID和变体ID - WooCommerce订单对象提供了大量
get_*方法,比如get_billing_address()、get_payment_method_title(),可以按需扩展返回字段 - 订单量较大时,优先用
WP_Query的筛选方式,性能更优
内容的提问来源于stack exchange,提问作者Balbant Singh
相关产品推荐
相关产品推荐

