WC_REST_Order_Refunds_V2_Controller::prepare_object_for_database()
Prepares one object for create or update operation.
Метод класса: WC_REST_Order_Refunds_V2_Controller{}
Хуки из метода
Возвращает
WP_Error|WC_Data
. The prepared item, or WP_Error object on failure.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->prepare_object_for_database( $request, $creating );
- $request(WP_REST_Request) (обязательный)
- Request object.
- $creating(true|false)
- If is creating a new object.
По умолчанию: false
Список изменений
С версии 3.0.0 | Введена. |
Код WC_REST_Order_Refunds_V2_Controller::prepare_object_for_database() WC REST Order Refunds V2 Controller::prepare object for database WC 9.3.3
protected function prepare_object_for_database( $request, $creating = false ) { $order = wc_get_order( (int) $request['order_id'] ); if ( ! $order ) { return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 ); } if ( 0 > $request['amount'] ) { return new WP_Error( 'woocommerce_rest_invalid_order_refund', __( 'Refund amount must be greater than zero.', 'woocommerce' ), 400 ); } // Create the refund. $refund = wc_create_refund( array( 'order_id' => $order->get_id(), 'amount' => $request['amount'], 'reason' => empty( $request['reason'] ) ? null : $request['reason'], 'refund_payment' => is_bool( $request['api_refund'] ) ? $request['api_refund'] : true, 'restock_items' => true, ) ); if ( is_wp_error( $refund ) ) { return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', $refund->get_error_message(), 500 ); } if ( ! $refund ) { return new WP_Error( 'woocommerce_rest_cannot_create_order_refund', __( 'Cannot create order refund, please try again.', 'woocommerce' ), 500 ); } if ( ! empty( $request['meta_data'] ) && is_array( $request['meta_data'] ) ) { foreach ( $request['meta_data'] as $meta ) { $refund->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); } $refund->save_meta_data(); } /** * Filters an object before it is inserted via the REST API. * * The dynamic portion of the hook name, `$this->post_type`, * refers to the object type slug. * * @since 4.5.0 * * @param WC_Data $coupon Object object. * @param WP_REST_Request $request Request object. * @param bool $creating If is creating a new object. */ $refund = apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating ); // If the filtered result is not a WC_Data instance and is not a WP_Error then something went wrong, but we // still need to honor the declared return type. return Types::ensure_instance_of( $refund, WC_Data::class, function ( $thing ) { return is_wp_error( $thing ) ? $thing : new WP_Error( 'woocommerce_rest_cannot_verify_refund_created', __( 'An unexpected error occurred while generating the refund.', 'woocommerce' ) ); } ); }