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']));
}
}
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 );
// Save the data
$order->save();
}
From a dynamic Order Id you can use WC_Orderadd_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 );
This code will do trick for you add code in functions.php
For data validation of the custom field use the code given below:
Update value of field
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
From a dynamic Order Id you can use
WC_Order
add_order_note()
method this way:Tested and works.