WC_API_Orders::create_order_note() public WC 2.2
Create a new order note for the given order
{} Это метод класса: WC_API_Orders{}
Возвращает
WP_Error/Массив. error or created note response data
Использование
$WC_API_Orders = new WC_API_Orders(); $WC_API_Orders->create_order_note( $order_id, $data );
- $order_id(строка) (обязательный)
- order ID
- $data(массив) (обязательный)
- raw request data
Список изменений
С версии 2.2 | Введена. |
Код WC_API_Orders::create_order_note() WC API Orders::create order note WC 5.0.0
public function create_order_note( $order_id, $data ) {
try {
if ( ! isset( $data['order_note'] ) ) {
throw new WC_API_Exception( 'woocommerce_api_missing_order_note_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'order_note' ), 400 );
}
$data = $data['order_note'];
// permission check
if ( ! current_user_can( 'publish_shop_orders' ) ) {
throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_order_note', __( 'You do not have permission to create order notes', 'woocommerce' ), 401 );
}
$order_id = $this->validate_request( $order_id, $this->post_type, 'edit' );
if ( is_wp_error( $order_id ) ) {
return $order_id;
}
$order = wc_get_order( $order_id );
$data = apply_filters( 'woocommerce_api_create_order_note_data', $data, $order_id, $this );
// note content is required
if ( ! isset( $data['note'] ) ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_order_note', __( 'Order note is required', 'woocommerce' ), 400 );
}
$is_customer_note = ( isset( $data['customer_note'] ) && true === $data['customer_note'] );
// create the note
$note_id = $order->add_order_note( $data['note'], $is_customer_note );
if ( ! $note_id ) {
throw new WC_API_Exception( 'woocommerce_api_cannot_create_order_note', __( 'Cannot create order note, please try again.', 'woocommerce' ), 500 );
}
// HTTP 201 Created
$this->server->send_status( 201 );
do_action( 'woocommerce_api_create_order_note', $note_id, $order_id, $this );
return $this->get_order_note( $order->get_id(), $note_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() ) );
}
}