admin管理员组文章数量:1290957
I'm setting up a silent auction to use at a fundraiser. All items are being picked up at the event, so I'm trying to get around having Billing and Shipping addresses, but I don't think I can do that. Seems that Wordpress and WooCommerce are trying to force the user into filling out those boxes during checkout. If I could auto populate the fields that are required, that would probably work for what i need.
So, as a work around, I'm trying to take the users First, Last, Email and Phone from their registration and just copy them to both the Billing and Shipping fields in the user's account.
I tried this code that I found searching Google;
add_filter('profile_update','custom_update_checkout_fields', 10, 2 );
function custom_update_checkout_fields($user_id, $old_user_data ) {
$current_user = wp_get_current_user();
// Updating Billing info
if($current_user->user_firstname != $current_user->billing_first_name)
update_user_meta($user_id, 'billing_first_name', $current_user->user_firstname);
if($current_user->user_lastname != $current_user->billing_last_name)
update_user_meta($user_id, 'billing_last_name', $current_user->user_lastname);
if($current_user->user_email != $current_user->billing_email)
update_user_meta($user_id, 'billing_email', $current_user->user_email);
// Updating Shipping info
if($current_user->user_firstname != $current_user->shipping_first_name)
update_user_meta($user_id, 'shipping_first_name', $current_user->user_firstname);
if($current_user->user_lastname != $current_user->shipping_last_name)
update_user_meta($user_id, 'shipping_last_name', $current_user->user_lastname);
if($current_user->user_email != $current_user->shipping_email)
update_user_meta($user_id, 'shipping_email', $current_user->user_email);
}
But, from what I can tell, this is only set to fire when a user updates their own profile. Running it as I created a user account didn't produce the results I was looking for.
I'm ok with PHP, but not with WordPress hooks.
Can someone take a quick peek at my code and point me in the right direction? Or, if there is a better way to accomplish what I am trying to do, that would be great too!
I'm setting up a silent auction to use at a fundraiser. All items are being picked up at the event, so I'm trying to get around having Billing and Shipping addresses, but I don't think I can do that. Seems that Wordpress and WooCommerce are trying to force the user into filling out those boxes during checkout. If I could auto populate the fields that are required, that would probably work for what i need.
So, as a work around, I'm trying to take the users First, Last, Email and Phone from their registration and just copy them to both the Billing and Shipping fields in the user's account.
I tried this code that I found searching Google;
add_filter('profile_update','custom_update_checkout_fields', 10, 2 );
function custom_update_checkout_fields($user_id, $old_user_data ) {
$current_user = wp_get_current_user();
// Updating Billing info
if($current_user->user_firstname != $current_user->billing_first_name)
update_user_meta($user_id, 'billing_first_name', $current_user->user_firstname);
if($current_user->user_lastname != $current_user->billing_last_name)
update_user_meta($user_id, 'billing_last_name', $current_user->user_lastname);
if($current_user->user_email != $current_user->billing_email)
update_user_meta($user_id, 'billing_email', $current_user->user_email);
// Updating Shipping info
if($current_user->user_firstname != $current_user->shipping_first_name)
update_user_meta($user_id, 'shipping_first_name', $current_user->user_firstname);
if($current_user->user_lastname != $current_user->shipping_last_name)
update_user_meta($user_id, 'shipping_last_name', $current_user->user_lastname);
if($current_user->user_email != $current_user->shipping_email)
update_user_meta($user_id, 'shipping_email', $current_user->user_email);
}
But, from what I can tell, this is only set to fire when a user updates their own profile. Running it as I created a user account didn't produce the results I was looking for.
I'm ok with PHP, but not with WordPress hooks.
Can someone take a quick peek at my code and point me in the right direction? Or, if there is a better way to accomplish what I am trying to do, that would be great too!
Share Improve this question asked Jun 12, 2019 at 1:38 RickRick 1033 bronze badges1 Answer
Reset to default 0But, from what I can tell, this is only set to fire when a user updates their own profile.
Yes, that's correct.
Running it as I created a user account didn't produce the results I was looking for.
The hook you should use when the user is being created/registered, is user_register
.
So you would use this, where you hook to both profile_update
and user_register
:
// Note that you should use add_action() and not add_filter()
add_action( 'profile_update', 'custom_update_checkout_fields' );
add_action( 'user_register', 'custom_update_checkout_fields' );
And since the second hook (user_register
) only provides one parameter, then just omit the $old_user_data
from the function; and secondly, you should use get_userdata()
and not wp_get_current_user()
to get the user being updated or created.
The full code I used:
add_action( 'profile_update', 'custom_update_checkout_fields' ); // Fires immediately after an existing user is updated.
add_action( 'user_register', 'custom_update_checkout_fields' ); // Fires immediately after a new user is registered.
function custom_update_checkout_fields( $user_id ) {
$current_user = get_userdata( $user_id ); // Here we use get_userdata() and not wp_get_current_user().
// Updating Billing info
if ( $current_user->user_firstname != $current_user->billing_first_name )
update_user_meta( $user_id, 'billing_first_name', $current_user->user_firstname );
if ( $current_user->user_lastname != $current_user->billing_last_name )
update_user_meta( $user_id, 'billing_last_name', $current_user->user_lastname );
if ( $current_user->user_email != $current_user->billing_email )
update_user_meta( $user_id, 'billing_email', $current_user->user_email );
// Updating Shipping info
if ( $current_user->user_firstname != $current_user->shipping_first_name )
update_user_meta( $user_id, 'shipping_first_name', $current_user->user_firstname );
if ( $current_user->user_lastname != $current_user->shipping_last_name )
update_user_meta( $user_id, 'shipping_last_name', $current_user->user_lastname );
if ( $current_user->user_email != $current_user->shipping_email )
update_user_meta( $user_id, 'shipping_email', $current_user->user_email );
}
Note though, the user_register
hook wouldn't be called if the customer is anonymous and not allowed to create a WordPress/user account.
Additional Code
You can use the woocommerce_checkout_get_value
hook to auto-fill the form data, although WooCommerce actually does the auto-fill if the metadata exist.
add_filter( 'woocommerce_checkout_get_value', 'custom_autofill_customer_data', 10, 2 );
function custom_autofill_customer_data( $value, $input ) {
$current_user = wp_get_current_user();
if ( $current_user ) {
switch ( $input ) {
case 'billing_first_name':
case 'shipping_first_name':
return $current_user->user_firstname;
case 'billing_last_name':
case 'shipping_last_name':
return $current_user->user_lastname;
case 'billing_email':
case 'shipping_email':
return $current_user->user_email;
}
}
return $value;
}
本文标签: WooCommerce RegistrationSync UserBilling and Shipping info
版权声明:本文标题:WooCommerce Registration, Sync User, Billing and Shipping info 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741524272a2383373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论