Solution:
first, you should add your custom fields like this: (use woocommerce_checkout_fields
filter)
function reigel_woocommerce_checkout_fields( $checkout_fields = array() ) {
$checkout_fields['order']['date_of_birth'] = array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Date of Birth'),
'placeholder' => __('dd/mm/yyyy'),
'required' => true,
);
return $checkout_fields;
}
add_filter( 'woocommerce_checkout_fields', 'reigel_woocommerce_checkout_fields' );
adding 'required'
and setting it to true
will have the same effect with how you check if this field is set or not. (your “Error checking”)
then in your woocommerce_checkout_update_user_meta
, the first parameter is not $order_id
but the $customer_id
. You should know too that the second parameter is $posted
. $posted
contains the $_POST[]
data. Including your custom fields if you did the code above.
function reigel_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {
if (isset($posted['date_of_birth'])) {
$dob = sanitize_text_field( $posted['date_of_birth'] );
update_user_meta( $customer_id, 'date_of_birth', $dob);
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );