Automattic\WooCommerce\Admin\API

Orders::get_formatted_item_data()protectedWC 1.0

Get formatted item data.

Method of the class: Orders{}

No Hooks.

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_formatted_item_data( $object );
$object(WC_Data) (required)
WC_Data instance.

Orders::get_formatted_item_data() code WC 9.3.3

protected function get_formatted_item_data( $object ) {
	$extra_fields = array( 'customer', 'products' );
	$fields       = false;
	// Determine if the response fields were specified.
	if ( ! empty( $this->request['_fields'] ) ) {
		$fields = wp_parse_list( $this->request['_fields'] );

		if ( 0 === count( $fields ) ) {
			$fields = false;
		} else {
			$fields = array_map( 'trim', $fields );
		}
	}

	// Initially skip line items if we can.
	$using_order_class_override = is_a( $object, '\Automattic\WooCommerce\Admin\Overrides\Order' );
	if ( $using_order_class_override ) {
		$data = $object->get_data_without_line_items();
	} else {
		$data = $object->get_data();
	}

	$extra_fields      = false === $fields ? array() : array_intersect( $extra_fields, $fields );
	$format_decimal    = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' );
	$format_date       = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' );
	$format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' );

	// Add extra data as necessary.
	$extra_data = array();
	foreach ( $extra_fields as $field ) {
		switch ( $field ) {
			case 'customer':
				$extra_data['customer'] = $this->get_customer_by_id( $data['customer_id'] );
				break;
			case 'products':
				$extra_data['products'] = $this->get_products_by_order_id( $object->get_id() );
				break;
		}
	}
	// Format decimal values.
	foreach ( $format_decimal as $key ) {
		$data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
	}

	// format total with order currency.
	if ( $object instanceof \WC_Order ) {
		$data['total_formatted'] = wp_strip_all_tags( html_entity_decode( $object->get_formatted_order_total() ), true );
	}

	// Format date values.
	foreach ( $format_date as $key ) {
		$datetime              = $data[ $key ];
		$data[ $key ]          = wc_rest_prepare_date_response( $datetime, false );
		$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
	}

	// Format the order status.
	$data['status'] = OrderUtil::remove_status_prefix( $data['status'] );

	// Format requested line items.
	$formatted_line_items = array();

	foreach ( $format_line_items as $key ) {
		if ( false === $fields || in_array( $key, $fields, true ) ) {
			if ( $using_order_class_override ) {
				$line_item_data = $object->get_line_item_data( $key );
			} else {
				$line_item_data = $data[ $key ];
			}
			$formatted_line_items[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $line_item_data ) );
		}
	}

	// Refunds.
	$data['refunds'] = array();
	foreach ( $object->get_refunds() as $refund ) {
		$data['refunds'][] = array(
			'id'     => $refund->get_id(),
			'reason' => $refund->get_reason() ? $refund->get_reason() : '',
			'total'  => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ),
		);
	}

	return array_merge(
		array(
			'id'                   => $object->get_id(),
			'parent_id'            => $data['parent_id'],
			'number'               => $data['number'],
			'order_key'            => $data['order_key'],
			'created_via'          => $data['created_via'],
			'version'              => $data['version'],
			'status'               => $data['status'],
			'currency'             => $data['currency'],
			'date_created'         => $data['date_created'],
			'date_created_gmt'     => $data['date_created_gmt'],
			'date_modified'        => $data['date_modified'],
			'date_modified_gmt'    => $data['date_modified_gmt'],
			'discount_total'       => $data['discount_total'],
			'discount_tax'         => $data['discount_tax'],
			'shipping_total'       => $data['shipping_total'],
			'shipping_tax'         => $data['shipping_tax'],
			'cart_tax'             => $data['cart_tax'],
			'total'                => $data['total'],
			'total_formatted'      => isset( $data['total_formatted'] ) ? $data['total_formatted'] : $data['total'],
			'total_tax'            => $data['total_tax'],
			'prices_include_tax'   => $data['prices_include_tax'],
			'customer_id'          => $data['customer_id'],
			'customer_ip_address'  => $data['customer_ip_address'],
			'customer_user_agent'  => $data['customer_user_agent'],
			'customer_note'        => $data['customer_note'],
			'billing'              => $data['billing'],
			'shipping'             => $data['shipping'],
			'payment_method'       => $data['payment_method'],
			'payment_method_title' => $data['payment_method_title'],
			'transaction_id'       => $data['transaction_id'],
			'date_paid'            => $data['date_paid'],
			'date_paid_gmt'        => $data['date_paid_gmt'],
			'date_completed'       => $data['date_completed'],
			'date_completed_gmt'   => $data['date_completed_gmt'],
			'cart_hash'            => $data['cart_hash'],
			'meta_data'            => $data['meta_data'],
			'refunds'              => $data['refunds'],
		),
		$formatted_line_items,
		$extra_data
	);
}