Updating User information on WordPress with Woocommerce?

I’m currently using WordPress and 2 plugins
http://www.wpexplorer.com/wordpress-user-contact-fields/
and
Woocommerce

I’m using an endpoint from woocommerce called form-edit-account.php, it lets the user edit his information, however.

Read More

The problem is, everytime I press update, everything gets updated properly but the phone number. I added everything inbetween the arterisks.

<?php wc_print_notices(); ?>

<form action="" method="post">

    <p class="form-row form-row-first">
        <label for="account_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="input-text" name="account_first_name" id="account_first_name" value="<?php echo esc_attr( $user->first_name ); ?>" />
    </p>
    <p class="form-row form-row-last">
        <label for="account_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="text" class="input-text" name="account_last_name" id="account_last_name" value="<?php echo esc_attr( $user->last_name ); ?>" />
    </p>
    <p class="form-row form-row-wide">
        <label for="account_email"><?php _e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="email" class="input-text" name="account_email" id="account_email" value="<?php echo esc_attr( $user->user_email ); ?>" />
    </p>
    <p class="form-row form-row-first">
        <label for="password_1"><?php _e( 'Password (leave blank to leave unchanged)', 'woocommerce' ); ?></label>
        <input type="password" class="input-text" name="password_1" id="password_1" />
    </p>
    <p class="form-row form-row-last">
        <label for="password_2"><?php _e( 'Confirm new password', 'woocommerce' ); ?></label>
        <input type="password" class="input-text" name="password_2" id="password_2" />
    </p>
    *****************
    <p class="form-row form-row-wide">
        <label for="account_phone"><?php _e( 'Phone', 'woocommerce' ); ?> </label>
        <input type="text" class="input-text" name="phone" id="phone" value="<?php echo esc_attr( $user->phone ); ?>" />
    </p>
    *****************
    <div class="clear"></div>

    <p><input type="submit" class="button" name="save_account_details" value="<?php _e( 'Save changes', 'woocommerce' ); ?>" /></p>

    <?php wp_nonce_field( 'save_account_details' ); ?>
    <input type="hidden" name="action" value="save_account_details" />
</form>

Is there anyway to keep using woocommerce to update this custom info? Sorry I’m new both to woocommerce and to user based websites.

Thanks

Related posts

Leave a Reply

2 comments

  1. @doublesharp

    There are a few errors in your code.

    update_usermeta
    

    is in fact

    update_user_meta
    

    and

    add_action("save_account_details",...
    

    should be

    add_action( 'woocommerce_save_account_details',...
    

    so the full code should be

    function save_additional_account_details( $user_ID ){
        $phone = ! empty( $_POST['phone'] ) ? wc_clean( $_POST['phone'] ) : '';
        update_user_meta( $user_ID, 'phone', $phone );
    }
    add_action( 'woocommerce_save_account_details', 'save_additional_account_details' );
    

    Took me a little trial&error to figure this out, but you gave me a right track, thanks!

    Cheers,
    Alex

  2. The save_account_details() function handles the form processing for form-edit-account.php. Once the default save is completed without errors, there is an action hook called woocommerce_save_account_details that you can use to save further information – it takes only the user ID as an argument, but the rest of the info is available in the $_POST.

    function save_additional_account_details( $user_ID ){
        $phone = ! empty( $_POST['phone'] ) ? wc_clean( $_POST['phone'] ) : '';
        update_usermeta( $user_ID, 'phone', $phone );
    }
    add_action( 'save_account_details', 'save_additional_account_details' );
    

    You also have a couple of issues with the code you added to the page:

    • The for attribute of the <label> tag has to match the name of the <input> tag, so “phone”
    • There is no reason to _e( 'Phone', 'woocommerce' ); unless “Phone” exists in the WooCommerce translations
    • You need to access the current value (which is going to be stored as User MetaData) using get_usermeta().
    • You should copy this file to one of your own name if you are going to modify it – upgrades to WooCommerce may overwrite your changes.

    Your additional code should look like the following.

    <p class="form-row form-row-wide">
        <label for="phone">Phone</label>
        <input type="text" class="input-text" name="phone" id="phone" value="<?php echo esc_attr( get_usermeta( $user->ID, 'phone' ) ); ?>" />
    </p>