WordPress at a glance

wc_get_order() WC 2.2

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.

Return

true/false/WC_Order/WC_Refund.

If the order is successfully received, it will return an instance of WC_Order object:

WC_Order Object (
	[order_type] => simple
	[id]    => 41863
	[post]  => WP_Post Object (
			[ID]                    => 41863
			[post_author]           => 1
			[post_date]             => 2017-12-14 00:29:56
			[post_date_gmt]         => 2017-12-13 21:29:56
			[post_content]          => 
			[post_title]            => Order – December 14, 2017 @ 12:29 ДП
			[post_excerpt]          => 
			[post_status]           => wc-pending
			[comment_status]        => open
			[ping_status]           => closed
			[post_password]         => order_5a319bd4d901c
			[post_name]             => zakaz-ndash-dec-13-2017-09-29-pm
			[to_ping]               => 
			[pinged]                => 
			[post_modified]         => 2017-12-14 00:29:56
			[post_modified_gmt]     => 2017-12-13 21:29:56
			[post_content_filtered] => 
			[post_parent]           => 0
			[guid]                  => http://auto.com/?post_type=shop_order&p=41863
			[menu_order]            => 0
			[post_type]             => shop_order
			[post_mime_type]        => 
			[comment_count]         => 0
			[filter]                => raw
		)

	[order_date]            => 2017-12-14 00:29:56
	[modified_date]         => 2017-12-14 00:29:56
	[customer_message]      => 
	[customer_note]         => 
	[post_status]           => wc-pending
	[prices_include_tax]    => 
	[tax_display_cart]      => excl
	[display_totals_ex_tax] => 1
	[display_cart_ex_tax]   => 1
	[billing_email]         => webgleb@gmail.com
	[formatted_billing_address:protected]  => 
	[formatted_shipping_address:protected] => 
)

Usage

wc_get_order( $the_order );
$the_order(mixed)
Post object or post ID of the order.
Default: false

Examples

#1 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'];

#2 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 object 'WC_Order' - 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 get_data() method allows to get protected data.

Code of wc_get_order: woocommerce/includes/wc-order-functions.php VER 3.5.2

<?php
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 );
}

Related Functions

From category: WooCommerce

No comments
    Hello, !     Log In . Register