2

I'm trying to add a custom field jckwds_date as an order note. I can't for the life of me figure out why this code isn't working in functions.php?

The code also only allows the note to be added in the are a certain role type.

function wdm_my_custom_notes_on_single_order_page($order){

    $user = wp_get_current_user();
    $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');

    if( array_intersect($allowed_roles, $user->roles ) )  {

        $value = get_post_meta( $product->get_id(), 'jckwds_date', true );

        echo $value;

        $order->add_order_note( $value, $is_customer_note = 1 );

    }
}

Basically I need THIS:

THIS

To be added HERE:

HERE

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Tommy
  • 23
  • 1
  • 5

6 Answers6

2

Update:

The following code will add from the order custom field 'jckwds_date' (or checkout posted field value 'jckwds_date') an order note that will appear in backend for defined user roles:

add_action( 'woocommerce_checkout_update_order_meta', 'product_custom_field_to_custom_order_notes', 100, 2 );
function product_custom_field_to_custom_order_notes( $order_id, $data ){
    // HERE define allowed user roles
    $allowed_roles = array('administrator', 'super_wholesale_customer', 'wholesale_customer');

    $user_id = get_post_meta( '_customer_user', 'jckwds_date', true );
    $user = new WP_User( $user_id );

    // Exit if no matched user roles
    if( ! array_intersect( $allowed_roles, $user->roles ) ) return;

    // Get the date custom field (or checkout field)
    if( get_post_meta( $order_id, 'jckwds_date', true ) ){
        $note = get_post_meta( $order_id, 'jckwds_date', true );
    } elseif( isset( $_POST['jckwds_date'] ) ){
        $note = sanitize_text_field( $_POST['jckwds_date'] );
    }

    // The order note
    if( isset($note) && ! empty($note) ){
        $order = wc_get_order( $order_id ); // The WC_Order Object
        $order->add_order_note( $note );  // Add the note
        $order->save(); // Save the order
    }
}

Code goes in function.php file of the active child theme (or active theme). It should work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0
add_filter( 'woocommerce_checkout_fields' , 'custom_add_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_add_checkout_fields( $fields ) {
     unset($fields['order']['order_comments']);
     $fields['order']['order_note']['priority'] = 5;
     $fields['order']['order_note'] = array(
        'label'     => __('Order Notes', 'woocommerce'),
    'placeholder'   => _x('Order Notes', 'placeholder', 'woocommerce'),
    'required'  => true,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );  
     return $fields;
}
0

Try this

$order = wc_get_order(  $order_id );


$note = __("This my custom note...");


$order->add_order_note( $note );

$order->save();
VinothRaja
  • 1,405
  • 10
  • 21
0

Try this

add_action('woocommerce_checkout_update_order_meta', 'checkout_field_update_order_meta');

                    function checkout_field_update_order_meta($order_id)
                    {
                        if (!empty($_POST['field_name'])) {
                            update_post_meta($order_id, 'MY Custom Field', sanitize_text_field($_POST['field_name']));
                        }
                    }
VinothRaja
  • 1,405
  • 10
  • 21
  • Hi, I don't see where it updates the notes? Just the order meta. The custom fields are working fine on the front end and saving in the back-end it's just adding them as a private note once they place the order. – Tommy Jul 24 '18 at 10:23
  • @Tommy You mean just add the custom private note after customer place the order?. It's only visible in admin panel. – VinothRaja Jul 24 '18 at 10:29
0

Try this code.

 add_action( 'woocommerce_thankyou', 'my_note_custom' );

    function my_note_custom( $order_id ) {

          $order = new WC_Order( $order_id );
          $note = __("This my custom note...");
          $order->add_order_note( $note );

          $order->save();

    }
VinothRaja
  • 1,405
  • 10
  • 21
  • Hi, I don't see where it updates the notes? Just the order meta. The custom fields are working fine on the front end and saving in the back-end it's just adding them as a private note once they place the order. – Tommy Jul 24 '18 at 11:11
  • @Tommy , have you found a way to send the cistom field as a private note? – Rosalito Udtohan Aug 12 '21 at 22:33
0

Found out it was a simple as changing the $product to $order as it is the order custom field value I'm trying to retrieve.

Full code below:

function wdm_my_custom_notes_on_single_order_page($order){

    $user = wp_get_current_user();
    $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');

    if( array_intersect($allowed_roles, $user->roles ) )  {

        $value = get_post_meta( $order->get_id(), 'jckwds_date', true );

        $note = '<b>Wholesale</b>';

        echo $value;

        $order->add_order_note( $value, $is_customer_note = 1 );

    }
}
Tommy
  • 23
  • 1
  • 5