In WooCommerce, at checkout, I am able to create or update all the user and billing information while placing an order. However, I want to set/update the user "display name" (alias) as well.
So based on "Capture custom checkout field value in Woocommerce" answer code, I have added the following code to my active theme's functions.php file:
// Display a custom checkout field
add_action( 'woocommerce_before_checkout_billing_form', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'display_name', array(
'type' => 'text',
'class' => array('my-custom-field form-row-wide'),
'label' => __('Alias'),
'placeholder' => __('Nickname to show in my account and comments'),
'required' => true,
), $checkout->get_value( 'display_name' ));
echo '</div>';
}
// Save the custom checkout field in the order meta
add_action( 'woocommerce_checkout_update_user_meta', 'save_order_custom_meta_data', 10, 2 );
function save_order_custom_meta_data( ) {
if ( isset($_POST['display_name']) )
$user->update_meta_data('display_name', sanitize_text_field( $_POST['display_name'] ) );
}
But this is not working as I get "Internal Server Error" message.
Any help to solve this will be appreciated.