Reading list Switch to dark mode

    How to Add Order Notes in WooCommerce Programmatically?

    Updated 31 May 2023

    In this dev blog article, we will look into adding order notes in WooCommerce and also see how can we add and delete an order note manually and programmatically.

    What is an order note?

    WooCommerce order note is a custom message for our future reference or for the customer. It can be added by the admin.

    Types of order notes

    There are two types of order notes.

    1. Private note
    2. Note for the customer.

    1. Private note: When we write some note for a particular order for our future reference but we don’t notify the customer then we add a private note.

    2. Note for the customer: When we have to send a note to the customer and notify them then we add a note for the customer.

    Start your headless eCommerce
    now.
    Find out More

    This is all about the WooCommerce order notes and types of order notes. Now move towards the add order note.

    How to add order notes manually?

    In this section, we will look into how to add order notes manually.

    Go to WooCommerce—> Orders —> Click on any order that which you want to add a note.

    Here you will see a section of Order Notes on the right-hand side. Add your message in the textarea and select the type of note and click on the add button to add the note for that order.

    If you add a note for the customer then the customer will notify through the email.

    add order notes in WooCommerce

    If you want to delete any of the order notes then simply click on the Delete note link which will appear below the order note as you can see in the above attached image.

    How to add and delete WooCommerce order notes programmatically?

    In this section, we will look into how to add and delete order notes programmatically.

    1. Add Order Note

    There are several ways to add notes in the order

    $order_note = 'Your order note here';
    
    /**
     * First method to add order note programmatically in woocommerce.
     * Using the <strong>WC_Order </strong>class.
     */
     $order = new WC_Order( $order_id ); 
     $order->add_order_note( $order_note ); // This will add as a private note.
     $order->add_order_note( $order_note, 1 ); //This will add note for the customer.
    
    /**
     * Second method to add order note programmatically in woocommerce.
     * If you don't want to use WC_Order class then you can use
     */
     $order = wc_get_order( $order->get_id() );
     $order->add_order_note( $order_note ); // This will add as a private note.
     $order->add_order_note( $order_note, 1 ); //This will add note for the customer.
    
    
    /**
     * Third method to add order note programmatically in woocommerce.
     * You can also add order note on hook while creating order note.
     */
     add_action( 'woocommerce_new_order', 'add_custom_order_note',  10, 1  );
    
        function add_custom_order_note( $order_id ) {
           
           $order = new WC_Order( $order_id ); 
           $order->add_order_note( $order_note ); // This will add as a private note.
           $order->add_order_note( $order_note, 1 ); //This will add note for          the customer.
        }

    2. Get all WooCommerce order notes programmatically.

    /**
     * Get all order notes of a particular order id.
     *
     * @param int $order_id Order id.
     * 
     * @return array $order_notes All order notes that belogs to the given order id. 
     */
    function get_order_notes( $order_id ) {
        remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
    
    	$comments = get_comments(
    		array(
    			'post_id' => $order_id,
    			'orderby' => 'comment_ID',
    			'order'   => 'DESC',
    			'approve' => 'approve',
    			'type'    => 'order_note',
    		)
    	);
    
    	$order_notes = wp_list_pluck( $comments, 'comment_content', 'comment_ID' );
    
        // You can also use the below code to get notes.
        //$notes = get_comments( $comments )
    
    
    	foreach ( $order_notes as $key => $note ) {
    		$is_customer = get_comment_meta( $key, 'is_customer_note', true );
    		$order_notes[ $key ] = array(
                'note_id'    => $key
    	        'order_note' => $note,
    	        'note_type'  => $is_customer,
    		);
    	}
    
    	add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
    	
    	return $order_notes;
    }

    Just call the any one above function and pass the order id. It will return the all order notes that belongs to the given order id.

    Note – If you have comment_id then you can use wc_get_order_note( $comment_id ) to retrieve the order note.

    Delete Order Notes.

    /**
     * Here is the code to delete the order note.
     *
     * wp_delete_comment is the core function of wordpress.
     */
    wp_delete_comment( $comment_id );

    // If you want to do something after the delete the comment then you can add your action on the below do action hook.

    do_action( ‘delete_comment’, string $comment_id, WP_Comment $comment )

    Conclusion

    Thanks for reading this dev blog, so if you have read the entire blog thoroughly then you are able to manage the order notes by yourself either manually or programmatically.

    Furthermore, if you have any doubts or queries related to this blog then kindly comment in the comment section.

    We will revert you as soon as possible on the same.

    WooCoomerce Support

    If you need any kind of support or you want to grow your business systematically and hassle-free then kindly visit the WooCommerce plugins page to see our add-ons. For any technical assistance, please raise a ticket or reach us by mail at [email protected].

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content