0

Good morning, I wrote/modified the following code to create 2 different new fileds on woocommerce (bithdate, and city of bith). Everything works like a charm (in admin page, in my account page) but I'd like to show them on checkout page (so at the first time the customer buy something I can collect that info too [required]).

function action_woocommerce_edit_account_form() {   
woocommerce_form_field( 'birthday_field', array(
    'type'        => 'date',
    'label'       => __( 'Data di nascita', 'woocommerce' ),
    'placeholder' => __( 'Data di nascita', 'woocommerce' ),
    'required'    => true,
), get_user_meta( get_current_user_id(), 'birthday_field', true ));
 woocommerce_form_field( 'birthcity_field', array(
    'type'        => 'text',
    'label'       => __( 'Città di nascita', 'woocommerce' ),
    'placeholder' => __( 'Città di nascita', 'woocommerce' ),
    'required'    => true,
), get_user_meta( get_current_user_id(), 'birthcity_field', true )); 
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['birthday_field']) && empty($_POST['birthday_field']) ) {
    $args->add( 'error', __( 'Prego inserire data di nascita', 'woocommerce' ) );
}
if ( isset($_POST['birthcity_field']) && empty($_POST['birthcity_field']) ) {
    $args->add( 'error', __( 'Prego inserire città di nascita', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account
function action_woocommerce_save_account_details( $user_id ) {  
if( isset($_POST['birthday_field']) && ! empty($_POST['birthday_field']) ) {
    update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
if( isset($_POST['birthcity_field']) && ! empty($_POST['birthcity_field']) ) {
    update_user_meta( $user_id, 'birthcity_field', sanitize_text_field($_POST['birthcity_field']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
// Add field - admin
function add_user_birtday_field( $user ) {
?>
    <h3><?php _e('Dati aggiuntivi','woocommerce' ); ?></h3>
    <table class="form-table">
        <tr>
            <th><label for="birthday_field"><?php _e( 'Data di nascita', 'woocommerce' ); ?></label></th>
            <td><input type="date" name="birthday_field" value="<?php echo esc_attr( get_the_author_meta( 'birthday_field', $user->ID )); ?>" class="regular-text" /></td>
        </tr>
    </table>
    <br />
    
    <table class="form-table">
        <tr>
            <th><label for="birthcity_field"><?php _e( 'Città di nascita', 'woocommerce' ); ?></label></th>
            <td><input type="text" name="birthcity_field" value="<?php echo esc_attr( get_the_author_meta( 'birthcity_field', $user->ID )); ?>" class="regular-text" /></td>
        </tr>
    </table>
    <br />
<?php
}
add_action( 'show_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'show_user_profile', 'add_user_birthcity_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birthcity_field', 10, 1 );
// Save field - admin
function save_user_birtday_field( $user_id ) {
if( ! empty($_POST['birthday_field']) ) {
    update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
}
if( ! empty($_POST['birthcity_field']) ) {
    update_user_meta( $user_id, 'birthcity_field', sanitize_text_field( $_POST['birthcity_field'] ) );
}
}
add_action( 'personal_options_update', 'save_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birtday_field', 10, 1 );
add_action( 'personal_options_update', 'save_user_birthcity_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birthcity_field', 10, 1 );

I'll be very gratefull to everyone who can help me. Warm regards Ale

  • If you are using existing code, please cite the [source](https://stackoverflow.com/a/62140927/11987538), because it gives proper credit to the author. – 7uc1f3r Sep 26 '21 at 10:32

1 Answers1

0

I solved by myself inspired by this article Post on Stackoverflow, BUT as I usual do I'd like to share the solution because could be helpful for someone.

Based on the code posted above You can add the field on the checkout page with the following code:

// Add the field to the checkout
add_action( 'woocommerce_after_checkout_billing_form', 'birth_day_checkout_field' );
function birth_day_checkout_field( $checkout ) {
// if customer is logged in and his birth date is defined
if(!empty(get_user_meta( get_current_user_id(), 'birthday_field', true ))){
$checkout_birht_date = esc_attr(get_user_meta( get_current_user_id(), 'birthday_field', true ));
}
// The customer is not logged in or his birth date is NOT defined
else {
    $checkout_birht_date = $checkout->get_value( 'birthday_field' );
}
echo '<p id="custom_birthday_field">';
woocommerce_form_field( 'birthday_field', array(
    'type'          => 'date',
    'class'         => array('birth-date form-row-wide'),
    'label'         => __( 'Data di nascita', 'woocommerce' ),
    'placeholder'   => __( 'mm/dd/yyyy', 'woocommerce' ),
    'required'    => true,
    ), $checkout_birht_date);
echo '</p>';
}
// Process the checkout
add_action('woocommerce_checkout_process','birth_day_checkout_field_process');
function birth_day_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['birthday_field'] )
wc_add_notice( __( 'Inserire la <strong>Data di nascita</strong>', 'woocommerce' ), 'error' )
;
}
// update order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'birth_day_checkout_field_update_order_meta' );
function birth_day_checkout_field_update_order_meta( $order_id ) {
if (!empty( $_POST['birthday_field'])){
    update_post_meta( $order_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
// updating user meta (for customer my account edit details page post data)

update_user_meta( get_post_meta( $order_id, '_customer_user', true ), 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}
// update user meta with Birth date (in checkout and my account edit details pages)
add_action ( 'personal_options_update', 'birth_day_save_extra_profile_fields' );
add_action ( 'edit_user_profile_update', 'birth_day_save_extra_profile_fields' );
add_action( 'woocommerce_save_account_details', 'birth_day_save_extra_profile_fields' );
function birth_day_save_extra_profile_fields( $user_id )
{
// for checkout page post data
if ( isset($_POST['birthday_field']) ) {
    update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
// for customer my account edit details page post data
if ( isset($_POST['birthday_field']) ) {
    update_user_meta( $user_id, 'birthday_field', sanitize_text_field($_POST['birthday_field']) );
}
}

In this way this data will be updated everywhere (my account, admin and checkout). Warm regards Ale