This is the code I am using:
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="personalisecookie"><h2>' . __('Personalise your cookie') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Our cookies can be personalised *up to 25 characters.'),
'placeholder' => __('Enter your details here'),
), $checkout->get_value( 'personalisation' ));
echo '</div>';
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['personalisation'] ) ) {
update_post_meta( $order_id, 'personalisation', sanitize_text_field( $_POST['personalisation'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('personalisation').':</strong> ' . get_post_meta( $order->id, 'personalisation', true ) . '</p>';
}
[Moderator Note: Please post code between backticks or use the code button. Your posted code may now have been damaged by the forum’s parser.]