WC_Order::add_order_note
Adds a note (comment) to the order. Order must exist.
Method of the class: WC_Order{}
Hooks from the method
Returns
Int. Comment ID.
Usage
$WC_Order = new WC_Order(); $WC_Order->add_order_note( $note, $is_customer_note, $added_by_user, $meta_data );
- $note(string) (required)
- Note to add.
- $is_customer_note(int)
- Is this a note for the customer?.
- $added_by_user(true|false)
- Was the note added by a user?.
Default:false - $meta_data(array)
- Optional meta data to add to the note. Key value pairs.
Default:array()
WC_Order::add_order_note() WC Order::add order note code WC 10.5.0
public function add_order_note( $note, $is_customer_note = 0, $added_by_user = false, $meta_data = array() ) {
if ( ! $this->get_id() ) {
return 0;
}
if ( is_user_logged_in() && current_user_can( 'edit_shop_orders', $this->get_id() ) && $added_by_user ) {
$user = get_user_by( 'id', get_current_user_id() );
$comment_author = $user->display_name;
$comment_author_email = $user->user_email;
} else {
$comment_author = __( 'WooCommerce', 'woocommerce' );
$comment_author_email = strtolower( __( 'WooCommerce', 'woocommerce' ) ) . '@';
$comment_author_email .= isset( $_SERVER['HTTP_HOST'] ) ? str_replace( 'www.', '', sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : 'noreply.com'; // WPCS: input var ok.
$comment_author_email = sanitize_email( $comment_author_email );
}
$commentdata = apply_filters(
'woocommerce_new_order_note_data',
array(
'comment_post_ID' => $this->get_id(),
'comment_author' => $comment_author,
'comment_author_email' => $comment_author_email,
'comment_author_url' => '',
'comment_content' => $note,
'comment_agent' => 'WooCommerce',
'comment_type' => 'order_note',
'comment_parent' => 0,
'comment_approved' => 1,
),
array(
'order_id' => $this->get_id(),
'is_customer_note' => $is_customer_note,
)
);
$comment_id = wp_insert_comment( $commentdata );
if ( $is_customer_note ) {
add_comment_meta( $comment_id, 'is_customer_note', 1 );
do_action(
'woocommerce_new_customer_note',
array(
'order_id' => $this->get_id(),
'customer_note' => $commentdata['comment_content'],
)
);
}
if ( ! empty( $meta_data ) && is_array( $meta_data ) ) {
foreach ( $meta_data as $key => $value ) {
if ( is_scalar( $value ) ) {
update_comment_meta( $comment_id, sanitize_key( $key ), sanitize_text_field( $value ) );
}
}
}
/**
* Action hook fired after an order note is added.
*
* @param int $order_note_id Order note ID.
* @param WC_Order $order Order data.
*
* @since 4.4.0
*/
do_action( 'woocommerce_order_note_added', $comment_id, $this );
return $comment_id;
}