In WooCommerce from the following line code:
$order = new WC_Order( $order_id );
How can I get WooCommerce order details from the order id
ONLY FOR WOOCOMMERCE VERSIONS 2.5.x AND 2.6.x
For WOOCOMMERCE VERSION 3.0+ see THIS UPDATE
Here is a custom function I have made, to make the things clear for you, related to get the data of an order ID. You will see all the different RAW outputs you can get and how to get the data you need…
Using print_r()
function (or var_dump()
function too) allow to output the raw data of an object or an array.
So first I output this data to show the object or the array hierarchy. Then I use different syntax depending on the type of that variable (string, array or object) to output the specific data needed.
IMPORTANT: With
$order
object you can use most of WC_order or WC_Abstract_Order methods (using the object syntax)…
Here is the code:
function get_order_details($order_id){
// 1) Get the Order object
$order = wc_get_order( $order_id );
// OUTPUT
echo 'RAW OUTPUT OF THE ORDER OBJECT:
';
print_r($order);
echo '
';
echo 'THE ORDER OBJECT (Using the object syntax notation):
';
echo '$order->order_type: ' . $order->order_type . '
';
echo '$order->id: ' . $order->id . '
';
echo 'THE POST OBJECT:
';
echo '$order->post->ID: ' . $order->post->ID . '
';
echo '$order->post->post_author: ' . $order->post->post_author . '
';
echo '$order->post->post_date: ' . $order->post->post_date . '
';
echo '$order->post->post_date_gmt: ' . $order->post->post_date_gmt . '
';
echo '$order->post->post_content: ' . $order->post->post_content . '
';
echo '$order->post->post_title: ' . $order->post->post_title . '
';
echo '$order->post->post_excerpt: ' . $order->post->post_excerpt . '
';
echo '$order->post->post_status: ' . $order->post->post_status . '
';
echo '$order->post->comment_status: ' . $order->post->comment_status . '
';
echo '$order->post->ping_status: ' . $order->post->ping_status . '
';
echo '$order->post->post_password: ' . $order->post->post_password . '
';
echo '$order->post->post_name: ' . $order->post->post_name . '
';
echo '$order->post->to_ping: ' . $order->post->to_ping . '
';
echo '$order->post->pinged: ' . $order->post->pinged . '
';
echo '$order->post->post_modified: ' . $order->post->post_modified . '
';
echo '$order->post->post_modified_gtm: ' . $order->post->post_modified_gtm . '
';
echo '$order->post->post_content_filtered: ' . $order->post->post_content_filtered . '
';
echo '$order->post->post_parent: ' . $order->post->post_parent . '
';
echo '$order->post->guid: ' . $order->post->guid . '
';
echo '$order->post->menu_order: ' . $order->post->menu_order . '
';
echo '$order->post->post_type: ' . $order->post->post_type . '
';
echo '$order->post->post_mime_type: ' . $order->post->post_mime_type . '
';
echo '$order->post->comment_count: ' . $order->post->comment_count . '
';
echo '$order->post->filter: ' . $order->post->filter . '
';
echo 'THE ORDER OBJECT (again):
';
echo '$order->order_date: ' . $order->order_date . '
';
echo '$order->modified_date: ' . $order->modified_date . '
';
echo '$order->customer_message: ' . $order->customer_message . '
';
echo '$order->customer_note: ' . $order->customer_note . '
';
echo '$order->post_status: ' . $order->post_status . '
';
echo '$order->prices_include_tax: ' . $order->prices_include_tax . '
';
echo '$order->tax_display_cart: ' . $order->tax_display_cart . '
';
echo '$order->display_totals_ex_tax: ' . $order->display_totals_ex_tax . '
';
echo '$order->display_cart_ex_tax: ' . $order->display_cart_ex_tax . '
';
echo '$order->formatted_billing_address->protected: ' . $order->formatted_billing_address->protected . '
';
echo '$order->formatted_shipping_address->protected: ' . $order->formatted_shipping_address->protected . '
';
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
';
// 2) Get the Order meta data
$order_meta = get_post_meta($order_id);
echo 'RAW OUTPUT OF THE ORDER META DATA (ARRAY):
';
print_r($order_meta);
echo '
';
echo 'THE ORDER META DATA (Using the array syntax notation):
';
echo '$order_meta[_order_key][0]: ' . $order_meta[_order_key][0] . '
';
echo '$order_meta[_order_currency][0]: ' . $order_meta[_order_currency][0] . '
';
echo '$order_meta[_prices_include_tax][0]: ' . $order_meta[_prices_include_tax][0] . '
';
echo '$order_meta[_customer_user][0]: ' . $order_meta[_customer_user][0] . '
';
echo '$order_meta[_billing_first_name][0]: ' . $order_meta[_billing_first_name][0] . '
';
echo 'And so on ………
';
echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
';
// 3) Get the order items
$items = $order->get_items();
echo 'RAW OUTPUT OF THE ORDER ITEMS DATA (ARRAY):
';
foreach ( $items as $item_id => $item_data ) {
echo 'RAW OUTPUT OF THE ORDER ITEM NUMBER: '. $item_id .'):
';
print_r($item_data);
echo '
';
echo 'Item ID: ' . $item_id. '
';
echo '$item_data["product_id"] (product ID): ' . $item_data['product_id'] . '
';
echo '$item_data["name"] (product Name): ' . $item_data['name'] . '
';
// Using get_item_meta() method
echo 'Item quantity (product quantity): ' . $order->get_item_meta($item_id, '_qty', true) . '
';
echo 'Item line total (product quantity): ' . $order->get_item_meta($item_id, '_line_total', true) . '
';
echo 'And so on ………
';
echo '- - - - - - - - - - - - -
';
}
echo '- - - - - - E N D - - - - -
';
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Usage (if your order ID is 159 for example):
get_order_details(159);
This code is tested and works.
Updated code on November 21, 2016