WordPress at Your Fingertips
function is not described

WC_REST_Legacy_Orders_Controller::prepare_item_for_response() public WC 1.0

Deprecated from version 3.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.

Prepare a single order output for response.

{} It's a method of the class: WC_REST_Legacy_Orders_Controller{}

Hooks from the method
Return

WP_REST_Response. $data

Usage

$WC_REST_Legacy_Orders_Controller = new WC_REST_Legacy_Orders_Controller();
$WC_REST_Legacy_Orders_Controller->prepare_item_for_response( $post, $request );
$post(WP_Post) (required)
Post object.
$request(WP_REST_Request) (required)
Request object.

Changelog

Deprecated 3.0

Code of WC_REST_Legacy_Orders_Controller::prepare_item_for_response() WC 5.7.1

public function prepare_item_for_response( $post, $request ) {
	$this->request       = $request;
	$this->request['dp'] = is_null( $this->request['dp'] ) ? wc_get_price_decimals() : absint( $this->request['dp'] );
	$statuses            = wc_get_order_statuses();
	$order               = wc_get_order( $post );
	$data                = array_merge( array( 'id' => $order->get_id() ), $order->get_data() );
	$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' );

	// Format decimal values.
	foreach ( $format_decimal as $key ) {
		$data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] );
	}

	// Format date values.
	foreach ( $format_date as $key ) {
		$data[ $key ] = $data[ $key ] ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $data[ $key ] ) ) ) : false;
	}

	// Format the order status.
	$data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status'];

	// Format line items.
	foreach ( $format_line_items as $key ) {
		$data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) );
	}

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

	$context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
	$data     = $this->add_additional_fields_to_object( $data, $request );
	$data     = $this->filter_response_by_context( $data, $context );
	$response = rest_ensure_response( $data );
	$response->add_links( $this->prepare_links( $order, $request ) );

	/**
	 * Filter the data for a response.
	 *
	 * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
	 * prepared for the response.
	 *
	 * @param WP_REST_Response   $response   The response object.
	 * @param WP_Post            $post       Post object.
	 * @param WP_REST_Request    $request    Request object.
	 */
	return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
}