WC_Order::add_order_note PHP Method

add_order_note() public method

Adds a note (comment) to the order. Order must exist.
public add_order_note ( string $note, integer $is_customer_note, $added_by_user = false ) : integer
$note string Note to add.
$is_customer_note integer (default: 0) Is this a note for the customer?
return integer Comment ID.
    public function add_order_note($note, $is_customer_note = 0, $added_by_user = false)
    {
        if (!$this->get_id()) {
            return 0;
        }
        if (is_user_logged_in() && current_user_can('edit_shop_order', $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.', '', $_SERVER['HTTP_HOST']) : 'noreply.com';
            $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']));
        }
        return $comment_id;
    }

Usage Example

 /**
  * Save tracking code.
  *
  * @param  int $post_id Current post type ID.
  *
  * @return void
  */
 public function save_tracking_code($post_id)
 {
     if (isset($_POST['correios_tracking'])) {
         $old = get_post_meta($post_id, 'correios_tracking', true);
         $new = $_POST['correios_tracking'];
         if ($new && $new != $old) {
             update_post_meta($post_id, 'correios_tracking', $new);
             // Gets order data.
             $order = new WC_Order($post_id);
             // Add order note.
             $order->add_order_note(sprintf(__('Added a Correios tracking code: %s', 'woocommerce-correios'), $new));
             // Send email notification.
             $this->trigger_email_notification($order, $new);
         } elseif ('' == $new && $old) {
             delete_post_meta($post_id, 'correios_tracking', $old);
         }
     }
 }
All Usage Examples Of WC_Order::add_order_note
WC_Order