I'm setting up a site with woocommerce login from frontend. I want to take the customer directly from there to registering billing address. Then I want to fill in the billing first and last names with the values registrered by user registration.

This code works, but I have to refresh the billing address page to make the name show in the fields. Why?

add_action("user_register", "pb_update_user_meta", 10, 2);
function pb_update_user_meta($user_id, $redirect_to) {
   global $filled;
   $user_id = get_current_user_id();
   $filled = get_user_meta( $user_id, 'billing_address_1', true );
   if($filled != '') {
       $redirect_to = home_url() . '/my-account/';
    } else {
       $user_first_name = get_user_meta('first_name');
       $user_last_name = get_user_meta('last_name');

       // setting up billing name
       update_user_meta( $user_id, 'billing_first_name', $user_first_name );
       update_user_meta( $user_id, 'billing_last_name', $user_last_name );

       $redirect_to = home_url() . '/my-account/edit-address/billing/';
   }

    return $redirect_to;

}

I have also tried the same by using woocommerce_login_redirect filter instead, but with the same result.

  • You can do a GET request to your redirect url. I.e. /my-account/edit-address/billing/?first_name=' . $user_first_name . '&last_name=' . $user_last_name. Do not forget to escape the values to avoid XSS attacks etc. – Laxmana Feb 7 '17 at 23:12
  • Good idea. But how do I retrieve these, and insert them into the address form? – knottulf Feb 8 '17 at 10:40
  • WooCommerce has a lot of templates that you can override. Find the template that renders the billing form, override it (child-theme etc) and inside the template file you can get your desired variable like this: $_GET['first_name'] – Laxmana Feb 8 '17 at 11:32

Sorry, I wasn't able to retrieve the $_GET variables. The solution was to retrieve the values from the database, and insert them into the proper places in the template file (form-edit-address.php) This works :-)

<?php 
    $user_id = get_current_user_id();
    $user_info = get_userdata( $user_id );
    $first_name = $user_info->first_name;
    $last_name = $user_info->last_name;
?>
<?php do_action("woocommerce_before_edit_address_form_{$load_address}" ); ?>

<?php 
      foreach ( $address as $key => $field ) : 
            if ($key == 'billing_first_name' && $field['value'] == '') 
                 woocommerce_form_field( $key, $field, $first_name );
            elseif ($key == 'billing_last_name' && $field['value'] == '')
                 woocommerce_form_field( $key, $field, $last_name ); 
            else 
                 woocommerce_form_field( $key, $field, ! empty( $_POST[ $key ] ) ? wc_clean( $_POST[ $key ] ) : $field['value'] );
        endforeach;

...

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.