按卖家姓名或作者姓名的 Woocommerce 订单
Woocommerce order by seller name or author name
我有以下代码,需要根据卖家id或卖家名称显示订单数据:
$filters = array(
'post_status' => 'published',
'post_type' => 'shop_order',
'posts_per_page' => 200,
'paged' => 1,
'orderby' => 'modified',
'order' => 'ASC',
'author' => $seller_id,
'post_parent' => $order_id
);
$loop = new WP_Query($filters);
while ($loop->have_posts()) {
$loop->the_post();
$order = new WC_Order($loop->post->ID);
foreach ($order->get_items() as $key => $item)
{
$red = $item['product_id'];
$_sku = get_post_meta( $red, '_sku', true );
}
}
上述循环未在发票部分显示任何 sku 或价格。
如何在作者 ID(或卖家姓名)上显示订单详情?
谢谢
您的主要问题是此处的 post_status
:对于 WooCommerce 订单 "published" post_status 不存在。 WooCommerce 订单的可用状态例如:
- '
wc-cancelled
'
- '
wc-completed
'
- '
wc-custom-status
'
- '
wc-on-hold
'
- '
wc-pending
'
- '
wc-processing
'
- '
wc-refunded
'
您可以在一个数组中包含不同的订单状态……
此外,'post_parent'
不能作为订单 ID,因为它始终具有 0
值
由于 WooCommerce 3+ 订单项现在是一个 WC_Order_Item_Product 对象(并且您需要使用可用的方法来访问属性值)。
最后,要从用户名中获取用户 ID,可以使用 WordPress 函数 get_user_by()
。
因此您的代码将是:
$args = array(
'post_status' => array( 'wc-completed' ), // <=== HERE
'post_type' => 'shop_order',
'posts_per_page' => 200,
'paged' => 1,
'orderby' => 'modified',
'author' => $seller_id
);
$loop = new WP_Query($args);
while ($loop->have_posts()) {
$loop->the_post();
$order_obj = wc_get_order($loop->post->ID);
foreach ($order_obj->get_items() as $item_id => $item_obj)
{
$item_data = $item_obj->get_data(); // Accessing WC_Order_Item_Product object protected data
$product_id = $item_data['product_id']; // Product ID
$product_sku = get_post_meta( $product_id, '_sku', true ); // SKU
// Just for Testing output
echo "Product ID is $product_id - Sku is $product_sku<br>";
}
}
此代码已经过测试,适用于 WooCommerce 3+
有用的答案:
我有以下代码,需要根据卖家id或卖家名称显示订单数据:
$filters = array(
'post_status' => 'published',
'post_type' => 'shop_order',
'posts_per_page' => 200,
'paged' => 1,
'orderby' => 'modified',
'order' => 'ASC',
'author' => $seller_id,
'post_parent' => $order_id
);
$loop = new WP_Query($filters);
while ($loop->have_posts()) {
$loop->the_post();
$order = new WC_Order($loop->post->ID);
foreach ($order->get_items() as $key => $item)
{
$red = $item['product_id'];
$_sku = get_post_meta( $red, '_sku', true );
}
}
上述循环未在发票部分显示任何 sku 或价格。
如何在作者 ID(或卖家姓名)上显示订单详情?
谢谢
您的主要问题是此处的 post_status
:对于 WooCommerce 订单 "published" post_status 不存在。 WooCommerce 订单的可用状态例如:
- '
wc-cancelled
' - '
wc-completed
' - '
wc-custom-status
' - '
wc-on-hold
' - '
wc-pending
' - '
wc-processing
' - '
wc-refunded
'
您可以在一个数组中包含不同的订单状态……
此外,'post_parent'
不能作为订单 ID,因为它始终具有 0
值
由于 WooCommerce 3+ 订单项现在是一个 WC_Order_Item_Product 对象(并且您需要使用可用的方法来访问属性值)。
最后,要从用户名中获取用户 ID,可以使用 WordPress 函数 get_user_by()
。
因此您的代码将是:
$args = array(
'post_status' => array( 'wc-completed' ), // <=== HERE
'post_type' => 'shop_order',
'posts_per_page' => 200,
'paged' => 1,
'orderby' => 'modified',
'author' => $seller_id
);
$loop = new WP_Query($args);
while ($loop->have_posts()) {
$loop->the_post();
$order_obj = wc_get_order($loop->post->ID);
foreach ($order_obj->get_items() as $item_id => $item_obj)
{
$item_data = $item_obj->get_data(); // Accessing WC_Order_Item_Product object protected data
$product_id = $item_data['product_id']; // Product ID
$product_sku = get_post_meta( $product_id, '_sku', true ); // SKU
// Just for Testing output
echo "Product ID is $product_id - Sku is $product_sku<br>";
}
}
此代码已经过测试,适用于 WooCommerce 3+
有用的答案: