WooCommerce add order note programmatically

To add a custom order note programmatically to a WooCommerce order (visible on the admin order details page), we need to insert the note after the user completes the checkout process.

Add Order Note in WooCommerce Programmatically

We can do this using the ‘woocommerce_thankyou’ action hook like the one below:

function add_custom_order_notes( $order_id ) {
	
	$order = wc_get_order(  $order_id );
	
	// Text for the note
	$note = __("This is my test note", 'text-domain');

	// Add note
	$order->add_order_note( $note );
}; 

add_action( 'woocommerce_thankyou', 'add_custom_order_notes', 10, 1 ); 

When the user completes the checkout process, the user is redirected to the WooCommerce thank you page, and then an action hook named woocommerce_thankyou is run. So the attached callback function can perform tasks, as shown above. First, it calls the wc_get_order( $order_id ) function to get the $order object and then calls the add_order_note() function associated with the $order object to add the order note.

Order Note Output Example

In the order details page, in the right sidebar area of the admin screen, you can see the order note as shown below:

Leave a Reply

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

Scroll to Top