WC_API_Orders::get_order_refund() public WC 2.2
Get an order refund for the given order ID and ID
{} It's a 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_refund( $order_id, $id, $fields, $filter );
- $order_id(string) (required)
- order ID
- $id(int) (required)
- -
- $fields(string|null)
- fields to limit response to
- $filter(array)
- -
Changelog
Since 2.2 | Introduced. |
Code of WC_API_Orders::get_order_refund() WC API Orders::get order refund WC 5.0.0
public function get_order_refund( $order_id, $id, $fields = null, $filter = array() ) {
try {
// Validate order ID
$order_id = $this->validate_request( $order_id, $this->post_type, 'read' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
}
$id = absint( $id );
if ( empty( $id ) ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 400 );
}
$order = wc_get_order( $order_id );
$refund = wc_get_order( $id );
if ( ! $refund ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'An order refund with the provided ID could not be found.', 'woocommerce' ), 404 );
}
$line_items = array();
// Add line items
foreach ( $refund->get_items( 'line_item' ) as $item_id => $item ) {
$product = $item->get_product();
$hideprefix = ( isset( $filter['all_item_meta'] ) && 'true' === $filter['all_item_meta'] ) ? null : '_';
$item_meta = $item->get_formatted_meta_data( $hideprefix );
foreach ( $item_meta as $key => $values ) {
$item_meta[ $key ]->label = $values->display_key;
unset( $item_meta[ $key ]->display_key );
unset( $item_meta[ $key ]->display_value );
}
$line_items[] = array(
'id' => $item_id,
'subtotal' => wc_format_decimal( $order->get_line_subtotal( $item ), 2 ),
'subtotal_tax' => wc_format_decimal( $item->get_subtotal_tax(), 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,
'meta' => array_values( $item_meta ),
'refunded_item_id' => (int) $item->get_meta( 'refunded_item_id' ),
);
}
$order_refund = array(
'id' => $refund->get_id(),
'created_at' => $this->server->format_datetime( $refund->get_date_created() ? $refund->get_date_created()->getTimestamp() : 0, false, false ),
'amount' => wc_format_decimal( $refund->get_amount(), 2 ),
'reason' => $refund->get_reason(),
'line_items' => $line_items,
);
return array( 'order_refund' => apply_filters( 'woocommerce_api_order_refund_response', $order_refund, $id, $fields, $refund, $order_id, $this ) );
} catch ( WC_API_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}