WC_REST_Order_Refunds_V1_Controller::prepare_item_for_response() │ public │ WC 1.0
Prepare a single order refund output for response.
Method of the class: WC_REST_Order_Refunds_V1_Controller{}
Return
WP_Error|WP_REST_Response
.
Usage
$WC_REST_Order_Refunds_V1_Controller = new WC_REST_Order_Refunds_V1_Controller();
$WC_REST_Order_Refunds_V1_Controller->prepare_item_for_response( $post, $request );
- $post(WP_Post) (required)
- Post object.
- $request(WP_REST_Request) (required)
- Request object.
WC_REST_Order_Refunds_V1_Controller::prepare_item_for_response() WC REST Order Refunds V1 Controller::prepare item for response code
WC 9.8.1
public function prepare_item_for_response( $post, $request ) {
$order = wc_get_order( (int) $request['order_id'] );
if ( ! $order ) {
return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 );
}
$refund = wc_get_order( $post );
if ( ! $refund || $refund->get_parent_id() !== $order->get_id() ) {
return new WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 );
}
$dp = is_null( $request['dp'] ) ? wc_get_price_decimals() : absint( $request['dp'] );
$data = array(
'id' => $refund->get_id(),
'date_created' => wc_rest_prepare_date_response( $refund->get_date_created() ),
'amount' => wc_format_decimal( $refund->get_amount(), $dp ),
'reason' => $refund->get_reason(),
'line_items' => array(),
);
// Add line items.
foreach ( $refund->get_items() as $item_id => $item ) {
$product = $item->get_product();
$product_id = 0;
$variation_id = 0;
$product_sku = null;
// Check if the product exists.
if ( is_object( $product ) ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product_sku = $product->get_sku();
}
$item_meta = array();
$hideprefix = 'true' === $request['all_item_meta'] ? null : '_';
foreach ( $item->get_all_formatted_meta_data( $hideprefix ) as $meta_key => $formatted_meta ) {
$item_meta[] = array(
'key' => $formatted_meta->key,
'label' => $formatted_meta->display_key,
'value' => wc_clean( $formatted_meta->display_value ),
);
}
$line_item = array(
'id' => $item_id,
'name' => $item['name'],
'sku' => $product_sku,
'product_id' => (int) $product_id,
'variation_id' => (int) $variation_id,
'quantity' => wc_stock_amount( $item['qty'] ),
'tax_class' => ! empty( $item['tax_class'] ) ? $item['tax_class'] : '',
'price' => wc_format_decimal( $refund->get_item_total( $item, false, false ), $dp ),
'subtotal' => wc_format_decimal( $refund->get_line_subtotal( $item, false, false ), $dp ),
'subtotal_tax' => wc_format_decimal( $item['line_subtotal_tax'], $dp ),
'total' => wc_format_decimal( $refund->get_line_total( $item, false, false ), $dp ),
'total_tax' => wc_format_decimal( $item['line_tax'], $dp ),
'taxes' => array(),
'meta' => $item_meta,
);
$item_line_taxes = maybe_unserialize( $item['line_tax_data'] );
if ( isset( $item_line_taxes['total'] ) ) {
$line_tax = array();
foreach ( $item_line_taxes['total'] as $tax_rate_id => $tax ) {
$line_tax[ $tax_rate_id ] = array(
'id' => $tax_rate_id,
'total' => $tax,
'subtotal' => '',
);
}
foreach ( $item_line_taxes['subtotal'] as $tax_rate_id => $tax ) {
$line_tax[ $tax_rate_id ]['subtotal'] = $tax;
}
$line_item['taxes'] = array_values( $line_tax );
}
$data['line_items'][] = $line_item;
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
// Wrap the data in a response object.
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $refund, $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 );
}