WC_API_Orders::get_order()publicWC 2.1

Get the order for the given ID

Method of the class: WC_API_Orders{}

Hooks from the method

Return

Array|WP_Error.

Usage

$WC_API_Orders = new WC_API_Orders();
$WC_API_Orders->get_order( $id, $fields );
$id(int) (required)
the order ID
$fields(array)
-
Default: null

Changelog

Since 2.1 Introduced.

WC_API_Orders::get_order() code WC 7.7.0

public function get_order( $id, $fields = null ) {

	// ensure order ID is valid & user has permission to read
	$id = $this->validate_request( $id, 'shop_order', 'read' );

	if ( is_wp_error( $id ) ) {
		return $id;
	}

	$order      = wc_get_order( $id );
	$order_data = array(
		'id'                        => $order->get_id(),
		'order_number'              => $order->get_order_number(),
		'created_at'                => $this->server->format_datetime( $order->get_date_created() ? $order->get_date_created()->getTimestamp() : 0, false, false ), // API gives UTC times.
		'updated_at'                => $this->server->format_datetime( $order->get_date_modified() ? $order->get_date_modified()->getTimestamp() : 0, false, false ), // API gives UTC times.
		'completed_at'              => $this->server->format_datetime( $order->get_date_completed() ? $order->get_date_completed()->getTimestamp() : 0, false, false ), // API gives UTC times.
		'status'                    => $order->get_status(),
		'currency'                  => $order->get_currency(),
		'total'                     => wc_format_decimal( $order->get_total(), 2 ),
		'subtotal'                  => wc_format_decimal( $this->get_order_subtotal( $order ), 2 ),
		'total_line_items_quantity' => $order->get_item_count(),
		'total_tax'                 => wc_format_decimal( $order->get_total_tax(), 2 ),
		'total_shipping'            => wc_format_decimal( $order->get_shipping_total(), 2 ),
		'cart_tax'                  => wc_format_decimal( $order->get_cart_tax(), 2 ),
		'shipping_tax'              => wc_format_decimal( $order->get_shipping_tax(), 2 ),
		'total_discount'            => wc_format_decimal( $order->get_total_discount(), 2 ),
		'cart_discount'             => wc_format_decimal( 0, 2 ),
		'order_discount'            => wc_format_decimal( 0, 2 ),
		'shipping_methods'          => $order->get_shipping_method(),
		'payment_details' => array(
			'method_id'    => $order->get_payment_method(),
			'method_title' => $order->get_payment_method_title(),
			'paid'         => ! is_null( $order->get_date_paid() ),
		),
		'billing_address' => array(
			'first_name' => $order->get_billing_first_name(),
			'last_name'  => $order->get_billing_last_name(),
			'company'    => $order->get_billing_company(),
			'address_1'  => $order->get_billing_address_1(),
			'address_2'  => $order->get_billing_address_2(),
			'city'       => $order->get_billing_city(),
			'state'      => $order->get_billing_state(),
			'postcode'   => $order->get_billing_postcode(),
			'country'    => $order->get_billing_country(),
			'email'      => $order->get_billing_email(),
			'phone'      => $order->get_billing_phone(),
		),
		'shipping_address' => array(
			'first_name' => $order->get_shipping_first_name(),
			'last_name'  => $order->get_shipping_last_name(),
			'company'    => $order->get_shipping_company(),
			'address_1'  => $order->get_shipping_address_1(),
			'address_2'  => $order->get_shipping_address_2(),
			'city'       => $order->get_shipping_city(),
			'state'      => $order->get_shipping_state(),
			'postcode'   => $order->get_shipping_postcode(),
			'country'    => $order->get_shipping_country(),
		),
		'note'                      => $order->get_customer_note(),
		'customer_ip'               => $order->get_customer_ip_address(),
		'customer_user_agent'       => $order->get_customer_user_agent(),
		'customer_id'               => $order->get_user_id(),
		'view_order_url'            => $order->get_view_order_url(),
		'line_items'                => array(),
		'shipping_lines'            => array(),
		'tax_lines'                 => array(),
		'fee_lines'                 => array(),
		'coupon_lines'              => array(),
	);

	// add line items
	foreach ( $order->get_items() as $item_id => $item ) {
		$product                    = $item->get_product();
		$order_data['line_items'][] = array(
			'id'         => $item_id,
			'subtotal'   => wc_format_decimal( $order->get_line_subtotal( $item ), 2 ),
			'total'      => wc_format_decimal( $order->get_line_total( $item ), 2 ),
			'total_tax'  => wc_format_decimal( $order->get_line_tax( $item ), 2 ),
			'price'      => wc_format_decimal( $order->get_item_total( $item ), 2 ),
			'quantity'   => $item->get_quantity(),
			'tax_class'  => $item->get_tax_class(),
			'name'       => $item->get_name(),
			'product_id' => $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id(),
			'sku'        => is_object( $product ) ? $product->get_sku() : null,
		);
	}

	// add shipping
	foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) {
		$order_data['shipping_lines'][] = array(
			'id'           => $shipping_item_id,
			'method_id'    => $shipping_item->get_method_id(),
			'method_title' => $shipping_item->get_name(),
			'total'        => wc_format_decimal( $shipping_item->get_total(), 2 ),
		);
	}

	// add taxes
	foreach ( $order->get_tax_totals() as $tax_code => $tax ) {
		$order_data['tax_lines'][] = array(
			'code'     => $tax_code,
			'title'    => $tax->label,
			'total'    => wc_format_decimal( $tax->amount, 2 ),
			'compound' => (bool) $tax->is_compound,
		);
	}

	// add fees
	foreach ( $order->get_fees() as $fee_item_id => $fee_item ) {
		$order_data['fee_lines'][] = array(
			'id'        => $fee_item_id,
			'title'     => $fee_item->get_name(),
			'tax_class' => $fee_item->get_tax_class(),
			'total'     => wc_format_decimal( $order->get_line_total( $fee_item ), 2 ),
			'total_tax' => wc_format_decimal( $order->get_line_tax( $fee_item ), 2 ),
		);
	}

	// add coupons
	foreach ( $order->get_items( 'coupon' ) as $coupon_item_id => $coupon_item ) {
		$order_data['coupon_lines'][] = array(
			'id'     => $coupon_item_id,
			'code'   => $coupon_item->get_code(),
			'amount' => wc_format_decimal( $coupon_item->get_discount(), 2 ),
		);
	}

	return array( 'order' => apply_filters( 'woocommerce_api_order_response', $order_data, $order, $fields, $this->server ) );
}