WC_API_Orders::create_order_note()
Create a new order note for the given order
Method of the class: WC_API_Orders{}
Hooks from the method
Return
WP_Error|Array
. error or created note response data
Usage
$WC_API_Orders = new WC_API_Orders(); $WC_API_Orders->create_order_note( $order_id, $data );
- $order_id(string) (required)
- order ID
- $data(array) (required)
- raw request data
Changelog
Since 2.2 | Introduced. |
WC_API_Orders::create_order_note() WC API Orders::create order note code WC 8.7.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() ) ); } }