wc_get_order()
Gets the data of the specified order in Woocommerce. This is a wrapper function to get the order object, through which you can get any data of the order and its elements.
Works based on the WC_Order_Factory class.
Not all order data can be obtained directly from the object, for some data you need to use special class methods (see examples).
No Hooks.
Returns
false|WC_Order|WC_Order_Refund.
If the order is successfully received, it will return an instance of WC_Order object:
Usage
wc_get_order( $the_order );
- $the_order(mixed)
- Post object or post ID of the order.
Default:false
Examples
#1 Order elements
To obtain order items, you need to use special methods that refer to the classes:
It means, that you cannot access the order items directly from the WC_Order object - only through special methods.
$order = wc_get_order( 65 );
$order_items = $order->get_items();
foreach( $order_items as $item_id => $item ){
// methods of WC_Order_Item class
// The element ID can be obtained from an array key or from:
$item_id = $item->get_id();
// methods of WC_Order_Item_Product class
$item_name = $item->get_name(); // Name of the product
$item_type = $item->get_type(); // Type of the order item ("line_item")
$product_id = $item->get_product_id(); // the Product id
$wc_product = $item->get_product(); // the WC_Product object
// order item data as an array
$item_data = $item->get_data();
echo $item_data['name'];
echo $item_data['product_id'];
echo $item_data['variation_id'];
echo $item_data['quantity'];
echo $item_data['tax_class'];
echo $item_data['subtotal'];
echo $item_data['subtotal_tax'];
echo $item_data['total'];
echo $item_data['total_tax'];
}
The WC_Order::get_data() method allows to get protected data.
#2 An example of retrieving order data
// get an instance of an WC_Order object $order_id = 35; $order = wc_get_order( $order_id ); // get the ID of the order $order_id = $order->get_id(); //> 35 // get the ID of the customer $order_id = $order->get_user_id(); //> 5 // get the order items, more details below... $order->get_items(); // ... etc.
Use WC_Order::get_data() method, to get order data as an array.
Attention! the get_data() method works from WC 3.0.
// the object WC_Order
$order = wc_get_order( 65 );
$data = $order->get_data(); // order data
echo $data['id'];
echo $data['parent_id'];
echo $data['status'];
echo $data['currency'];
echo $data['version'];
echo $data['payment_method'];
echo $data['payment_method_title'];
echo $data['payment_method'];
echo $data['payment_method'];
// get formatted date using the method date()
echo $data['date_created']->date('Y-m-d H:i:s');
echo $data['date_modified']->date('Y-m-d H:i:s');
// get the timestamp through the method getTimestamp()
echo $data['date_created']->getTimestamp();
echo $data['date_modified']->getTimestamp();
// more data
echo $data['discount_total'];
echo $data['discount_tax'];
echo $data['shipping_total'];
echo $data['shipping_tax'];
echo $data['cart_tax'];
echo $data['total_tax'];
echo $data['customer_id']; // ... and so on
// billing - account statement
echo $data['billing']['first_name'];
echo $data['billing']['last_name'];
echo $data['billing']['company'];
echo $data['billing']['address_1'];
echo $data['billing']['address_2'];
echo $data['billing']['city'];
echo $data['billing']['state'];
echo $data['billing']['postcode'];
echo $data['billing']['country'];
echo $data['billing']['email'];
echo $data['billing']['phone'];
// shipping
echo $data['shipping']['first_name'];
echo $data['shipping']['last_name'];
echo $data['shipping']['company'];
echo $data['shipping']['address_1'];
echo $data['shipping']['address_2'];
echo $data['shipping']['city'];
echo $data['shipping']['state'];
echo $data['shipping']['postcode'];
echo $data['shipping']['country'];
Changelog
| Since 2.2 | Introduced. |
wc_get_order() wc get order code WC 10.7.0
function wc_get_order( $the_order = false ) {
if ( ! did_action( 'woocommerce_after_register_post_type' ) ) {
wc_doing_it_wrong( __FUNCTION__, 'wc_get_order should not be called before post types are registered (woocommerce_after_register_post_type action)', '2.5' );
return false;
}
return WC()->order_factory->get_order( $the_order );
}