[英]Woocommerce Order By Billing Name
$orders= wc_get_orders( array(
'orderby' => 'billing_first_name',
'order' => 'ASC',
) );
除了使用
wc_get_orders
您还可以使用WP_Query
获取订单,然后形成ID,您可以创建Order Object然后进行填充。
像这样:
$args = array(
'numberposts' => -1, //to get all post, you can use Pagination
'post_type' => 'shop_order',
'post_status' => array_keys(wc_get_order_statuses()),
'meta_key' => '_billing_first_name', //order by first name
'orderby' => 'meta_value',
'order' => 'ASC',
'fields' => 'ids'
);
$orders = new WP_Query($args);
//echo '<pre>';
//print_r($orders->posts);
if (!empty($orders->posts))
{
foreach ($orders->posts as $order_id)
{
$order = wc_get_order($order_id); //getting order Object
//Now that you have the order object make the magic happen here.
echo $order->get_billing_first_name() . '<br>';
}
/* Restore original Post Data */
wp_reset_postdata();
}
else
{
// no posts found
}
print_r(array_keys(wc_get_order_statuses()));
将输出如下内容:
Array
(
[0] => wc-pending
[1] => wc-processing
[4] => wc-on-hold
[5] => wc-completed
[6] => wc-cancelled
[7] => wc-refunded
[8] => wc-failed
)
如果要按产品ID获取订单的订单,则必须使用自定义查询,因为WP_Query不适用于非自定义MySQL表。
这是替代方法:
/** * method to get the order ID(s) by product ID(s) * @param int|array $product_id * @return array */ function wh_getOrdersByProductId($product_id) { global $wpdb; if(empty($product_id)) return []; $operator = is_array($product_id) ? 'IN' : '='; $query = "SELECT p.ID AS order_id FROM {$wpdb->prefix}posts AS p INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id WHERE p.post_type = 'shop_order' AND p.post_status IN ('wc-pending', 'wc-completed', 'wc-processing', 'wc-on-hold', 'wc-cancelled', 'wc-refunded', 'wc-failed') AND pm.meta_key = '_billing_first_name' AND woim.meta_key = '_product_id' AND woim.meta_value {$operator} ({$product_id}) AND woi.order_item_type = 'line_item' ORDER BY pm.meta_key ASC;"; return $wpdb->get_col($wpdb->prepare($query)); }
代码进入您的活动子主题(或主题)的functions.php
文件中。 或在任何插件php文件中。
用法
//$orders = wh_getOrdersByProductId([37, 45]); $orders = wh_getOrdersByProductId(37); if (!empty($orders)) { foreach ($orders as $order_id) { $order = wc_get_order($order_id); //getting order Object //Now that you have the order object make the magic happen here. echo $order->get_billing_first_name() . '<br>'; } } else { // no posts found }
有用的链接:
代码已经过测试并且可以正常工作。
希望这可以帮助!
如果有人对这里的长版本感兴趣,我肯定有一个WP_Query版本要简单得多,我只是无法弄清楚。
SELECT order_items.order_id FROM {$wpdb->prefix}woocommerce_order_items as order_items LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id LEFT JOIN {$wpdb->postmeta} AS postmeta ON order_items.order_id = postmeta.post_id AND postmeta.meta_key = '_billing_first_name' LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID WHERE posts.post_type = 'shop_order' AND posts.post_status IN ( '" . implode( "','", $order_status ) . "' ) AND order_items.order_item_type = 'line_item' AND order_item_meta.meta_key = '_product_id' AND order_item_meta.meta_value = '$product_id' ORDER BY postmeta.meta_value DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.