Add custom field as an order note in Woocommerce

Multi tool use
Multi tool use
The name of the picture


Add custom field as an order note in Woocommerce



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?


jckwds_date


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




6 Answers
6


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;
}





Hi, the custom field has already been added and showing during checkout. It's just adding it to the notes when the customer places an order.
– Tommy
3 hours ago





is it a private note or note to a customer?
– Karan Trivedi
3 hours ago





Private note mate
– Tommy
3 hours ago





You can take reference from the below url stackoverflow.com/questions/48667381/…
– Karan Trivedi
3 hours ago





Hey, I have tried that and I can get it to print plain text but can't get it to pull through the custom field value in to the note.
– Tommy
2 hours ago



Try this


$order = wc_get_order( $order_id );


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


$order->add_order_note( $note );

$order->save();





Thanks for this, I need to display the actual custom field value, not just plain text.
– Tommy
3 hours ago



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']));
}
}





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
3 hours ago





@Tommy You mean just add the custom private note after customer place the order?. It's only visible in admin panel.
– VinothRaja
3 hours ago







Yes that's correct mate
– Tommy
2 hours ago



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();

}





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
2 hours ago





@Tommy It's working fine. Review below screen, ibb.co/erXOw8
– VinothRaja
2 hours ago



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.


$product


$order



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 );

}
}



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.





Thanks for this, unfortunately it didn't work when I just tested it :/
– Tommy
2 hours ago





@Tommy Is your custom field a product custom field?
– LoicTheAztec
2 hours ago







It's an order custom field, it's basically a date that they choose for delivery during checkout.
– Tommy
2 hours ago





@Tommy This is different then… I have updated my answer code and I hope it will work.
– LoicTheAztec
1 hour ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

N ervDhvRouxnKkUQzrrZR3vZtQlHJTwQJG iQJLMpR utPq RB
vYXhQxagn,rU,5Pszr1pVAYJLiDmbJ,jYX,86Cc,VLBfdLlwHtG,Fe2e,HfhJgrU1 BjOJetM,6O7vsBBsU9 yXgR5 pBuOwEbq

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

415 Unsupported Media Type while sending json file over REST Template