WC_API_Orders::edit_order_refund()publicWC 2.2

Edit an order refund

Method of the class: WC_API_Orders{}

Return

WP_Error|Array. error or edited refund response data

Usage

$WC_API_Orders = new WC_API_Orders();
$WC_API_Orders->edit_order_refund( $order_id, $id, $data );
$order_id(string) (required)
order ID
$id(string) (required)
refund ID
$data(array) (required)
parsed request data

Changelog

Since 2.2 Introduced.

WC_API_Orders::edit_order_refund() code WC 8.7.0

public function edit_order_refund( $order_id, $id, $data ) {
	try {
		if ( ! isset( $data['order_refund'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_missing_order_refund_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'order_refund' ), 400 );
		}

		$data = $data['order_refund'];

		// Validate order ID
		$order_id = $this->validate_request( $order_id, $this->post_type, 'edit' );

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

		// Validate refund ID
		$id = absint( $id );

		if ( empty( $id ) ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 400 );
		}

		// Ensure order ID is valid
		$refund = get_post( $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 );
		}

		// Ensure refund ID is associated with given order
		if ( $refund->post_parent != $order_id ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_order_refund_id', __( 'The order refund ID provided is not associated with the order.', 'woocommerce' ), 400 );
		}

		$data = apply_filters( 'woocommerce_api_edit_order_refund_data', $data, $refund->ID, $order_id, $this );

		// Update reason
		if ( isset( $data['reason'] ) ) {
			$updated_refund = wp_update_post( array( 'ID' => $refund->ID, 'post_excerpt' => $data['reason'] ) );

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

		// Update refund amount
		if ( isset( $data['amount'] ) && 0 < $data['amount'] ) {
			update_post_meta( $refund->ID, '_refund_amount', wc_format_decimal( $data['amount'] ) );
		}

		do_action( 'woocommerce_api_edit_order_refund', $refund->ID, $order_id, $this );

		return $this->get_order_refund( $order_id, $refund->ID );
	} catch ( WC_Data_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => 400 ) );
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}