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.
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;
...
/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$_GET['first_name']
– Laxmana Commented Feb 8, 2017 at 11:32