1
votes

I want to change some functions in woocommerce account-edit page. I want user_email or account_email fields equals to billing_email.

I displayed a billing_email field in Woocommerce Edit Account Page and then try to update_user_metaa but its not working.

// add_action( 'woocommerce_edit_account_form_start', 'add_billing_email_to_edit_account_form' ); // At start
add_action( 'woocommerce_edit_account_form_start', 'add_billing_details_to_edit_account_form' ); 
function add_billing_details_to_edit_account_form() {
    $user = wp_get_current_user();
    ?>

<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
<label for="billing_email"><?php _e( 'Email Address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--email input-text" name="billing_email" id="billing_email" value="<?php echo esc_attr( $user->billing_email ); ?>" />
</p>
<?php
}
add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_user', 20, 1 );
function my_account_saving_billing_user( $user_id ) {
if( isset($_POST['billing_email']) && ! empty($_POST['billing_email']) )    
update_user_meta( $user_id, 'account_email', sanitize_text_field($_POST['billing_email']) );    
}

I want when user update his billing_email from Woocommerce Edit Account page then user_email or account_email is also changed.

Both billing_email and user_email or account_email must be same.

1

1 Answers

0
votes

add this to your functions.php and when you change the account in one place or the other both will change, so both places will be the same. If you are missing any information let me know:

add_action( 'woocommerce_save_account_details', 'my_account_saving_billing_user', 20, 1 );

function my_account_saving_billing_user( $user_id ) {

    if( isset( $_POST['account_email'] ) && $_POST['account_email'] != '' ){
        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );
    }
}



add_action( 'woocommerce_customer_save_address', 'save_billing_email_to_user', 12, 1 );
function save_billing_email_to_user( $user_id ) {
    if( isset( $_POST['billing_email'] ) && $_POST['billing_email'] != '' ){
        $args = array(
            'ID'         => $user_id,
            'user_email' => sanitize_text_field( esc_attr( $_POST['billing_email'] )) 
        );
        wp_update_user( $args );
    }
}