woocommerce offtopic - Updating usermeta from login redirect to billing address

admin2025-01-08  4

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.

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.

Share Improve this question asked Feb 7, 2017 at 21:18 halhal 351 gold badge1 silver badge9 bronze badges 3
  • 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 Commented Feb 7, 2017 at 23:12
  • Good idea. But how do I retrieve these, and insert them into the address form? – hal Commented Feb 8, 2017 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 Commented Feb 8, 2017 at 11:32
Add a comment  | 

1 Answer 1

Reset to default 0

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;

...
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269848a1383.html

最新回复(0)