Woocommerce - php to get order information

2020-06-03 01:38发布

I'm trying to get the data associated with an order on the woocommerce plugin (wordpress). Currently, I have written my own plugin that contains the code:

<?php 
global $woocommerce;
$order = new WC_Order($order_id);
$order_shipping_total = $order->get_shipping();
echo $order_shipping_total;
?>

This is just to test it out, I don't believe this is working- BUT what I actually need is to get a list of the orders that have a certain order status, and then be able to access the fields(like first name) for each order in this list. How do I go about doing this? Also, what files do I include to make this work? The class-wc-order() file?

2条回答
Bombasti
2楼-- · 2020-06-03 02:13

To filter out orders for a particular customer use additional argument meta_value:

$user_id = get_current_user_id();
$args = array(
  'post_type' => 'shop_order',
  'post_status' => 'publish',
  'meta_key' => '_customer_user',
  'meta_value' => $user_id,
  'numberposts' => -1, // -1 for all orders
  'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);

Also alternative way for loading orders for a particular customer:

$orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
    'numberposts' => 1, // -1 for all orders
    'meta_key'    => '_customer_user',
    'meta_value'  => $user_id,
    'post_type'   => 'shop_order',
    'post_status' => 'publish'
) ) );

See also here.

查看更多
Emotional °昔
3楼-- · 2020-06-03 02:30

Recently i worked for Export of Orders Data in XML.

$args = array(
  'post_type' => 'shop_order',
  'post_status' => 'publish',
  'meta_key' => '_customer_user',
  'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);

$customer_orders = $my_query->posts;

foreach ($customer_orders as $customer_order) {
 $order = new WC_Order();

 $order->populate($customer_order);
 $orderdata = (array) $order;

 // $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
}
查看更多
登录 后发表回答