There is a mistake in your first function as each checkbox needs it own $key to be saved.
Here is your revisited code that will first validate that mandatory checkboxes, save values and display them in the admin User dashboard:
function registration_custom_checkboxes() {
return apply_filters( 'woocommerce_forms_field', array(
'age_ckeck' => array(
'type' => 'checkbox',
'label' => __("Soy mayor de 18", "woocommerce"),
'required' => true,
),
'permission_ckeck' => array(
'type' => 'checkbox',
'label' => __("Le doy permiso de usar mis datos", "woocommerce"),
'required' => true,
)
) );
}
// Display checkboxes in Registration form
add_action( 'woocommerce_register_form', 'add_registration_custom_checkboxes', 15 );
function add_registration_custom_checkboxes() {
foreach ( registration_custom_checkboxes() as $field_key => $field_args ) {
woocommerce_form_field( $field_key, $field_args );
}
}
// Validation for mandatory checkboxes in Registration form
add_action( 'woocommerce_register_post', 'registration_custom_checkboxes_validation', 10, 3 );
function registration_custom_checkboxes_validation( $username, $email, $validation_errors ) {
$fields = registration_custom_checkboxes();
foreach ( array_keys($fields) as $field_key ) {
if ( ! isset( $_POST[$field_key] ) {
if ( $field_key === 'age_ckeck' ) {
$validation_errors->add( $field_key . '_error', __("You need to approve that your are at leats 18 years old.", "woocommerce") );
} else {
$validation_errors->add( $field_key . '_error', __("You have to allow us to use your data.", "woocommerce") );
}
}
}
return $validation_errors;
}
// Save registration checkboxes fields values
add_action( 'woocommerce_created_customer', 'save_registration_custom_checkboxes_values' );
function save_registration_custom_checkboxes_values( $customer_id ) {
$fields = registration_custom_checkboxes();
foreach ( array_keys($fields) as $field_key ) {
if ( isset( $_POST[$field_key] ) ) {
update_user_meta( $customer_id, $field_key, '1' );
}
}
}
// Admin WP User: Display checkboxes fields on user profile
add_action ( 'show_user_profile', 'display_user_custom_checkboxes_fields' );
add_action ( 'edit_user_profile', 'display_user_custom_checkboxes_fields' );
function display_user_custom_checkboxes_fields( $user ){
echo '<h3>'.__("Age and permission checks", "woocommerce").'</h3>';
foreach ( registration_custom_checkboxes() as $field_key => $field_args ) {
woocommerce_form_field( $field_key, $field_args, get_user_meta( $user->id, $field_key, true ) );
}
}
// Admin WP User: Update checkboxes fields values
add_action( 'personal_options_update', 'save_user_custom_checkboxes_values' );
add_action( 'edit_user_profile_update', 'save_user_custom_checkboxes_values' );
function save_user_custom_checkboxes_values( $user_id ) {
$fields = registration_custom_checkboxes();
foreach ( array_keys($fields) as $field_key ) {
$value = isset( $_POST[$field_key] ) ? '1' : '0';
update_user_meta( $user_id, $field_key, $value );
// error_log( ('Key: ' . $field_key . ' | posted: ' . $_POST[$field_key] . ' | value: ' . $value) );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.