25

In woocommerce I am trying to add a custom order note in the admin order edit pages through php (so programmatically). I haven't find the way yet.

Any help will be appreciated.

WooCommerce order note in the order admin page

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Max
  • 803
  • 3
  • 10
  • 24

3 Answers3

68

From a dynamic Order Id you can use WC_Order add_order_note() method this way:

// If you don't have the WC_Order object (from a dynamic $order_id)
$order = wc_get_order(  $order_id );

// The text for the note
$note = __("This is my note's text…");

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

Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 4
    I did not need to save the order. It handles that in add_order_note(); – user3080408 Apr 23 '19 at 18:32
  • Is there a hook which I can use when a new customer notification was created both ways, manually and programmatically? – Mr. Jo May 22 '19 at 20:26
  • Can I use `set_customer_note` instead? Do you know what a customer note differ from a non-customer note? – Alex Jul 12 '19 at 17:48
  • Btw. How did you get the order_id or is the same as order number? I am using Woocommerce v3.9.1 , could you please guide me with a simple code. – Savio menezes Jan 29 '20 at 11:02
  • @Saviomenezes It's more complicated than that as I can't guess things behind with the provided comment. You should ask a new question, clear detailed with the code you have actually, explaining what you are trying to do, and what is not working. – LoicTheAztec Jan 29 '20 at 21:26
  • Thanks for the snippet!… Do you know how I could add an admin (private) note when the automatic transactional emails are sent? – 63N Sep 28 '20 at 07:27
  • Link to the doc has changed: https://woocommerce.github.io/code-reference/classes/WC-Order.html#method_add_order_note – Sébastien Serre Jan 04 '22 at 18:53
4

Thank you guys I was trying to find a way to add the notes to a new order. I was looking for the right hook using the solution that @LoicTheAztec posted. This is the solution that worked for me hope it helps someone else out there.

add this to the Functions.php file

add_action( 'woocommerce_new_order', 'add_engraving_notes',  1, 1  );

function add_engraving_notes( $order_id ) {
 //note this line is different 
 //because I already have the ID from the hook I am using.
 $order = new WC_Order( $order_id ); 

 // The text for the note
 $note = __("Custom Order Note Here");

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

}
Mansoorkhan Cherupuzha
  • 1,761
  • 1
  • 24
  • 45
DevTurtle
  • 478
  • 4
  • 11
  • 4
    I don't think you need `$order->save()` because add_order_note method updates the db for you already. – Alex Jul 12 '19 at 17:47
  • 1
    The method has two extra params: `add_order_note( $note, $is_customer_note = 0, $added_by_user = false )` – brasofilo Apr 30 '21 at 03:27
-3

This code will do trick for you add code in functions.php

<?php

add_action('woocommerce_after_order_notes', 'customise_checkout_field');

function customise_checkout_field($checkout)
{
    echo '<div id="customise_checkout_field"><h2>' . __('Heading') . '</h2>';
    woocommerce_form_field('customised_field_name', array(
        'type' => 'text',
        'class' => array(
        'my-field-class form-row-wide'
    ) ,
    'label' => __('Customise Additional Field') ,
    'placeholder' => __('Guidence') ,
    'required' => true,
    ) , $checkout->get_value('customised_field_name'));
    echo '</div>';
}

For data validation of the custom field use the code given below:

add_action('woocommerce_checkout_process', 'customise_checkout_field_process');
 
function customise_checkout_field_process()
{
    // if the field is set, if not then show an error message.
    if (!$_POST['customised_field_name']) wc_add_notice(__('Please enter value.') , 'error');
}

Update value of field

add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
                     
function customise_checkout_field_update_order_meta($order_id)
{
    if (!empty($_POST['customised_field_name'])) {
        update_post_meta($order_id, 'Some Field', sanitize_text_field($_POST['customised_field_name']));
    }
}
brasofilo
  • 25,496
  • 15
  • 91
  • 179
Zaheer Abbas
  • 145
  • 3
  • 13