Not sure how you want to pass it, but I think it should be possible to pass anything into this filter.
I think the WooCommerce user data is saved into cookie along with cart data so it should work.
As Suyon I’m looking to pass value from CF7 to some fields into Woocomerce, but fileds are in theproduct page. Is it possibile with a similar code?
yes you can, on submit you can store CF7 data into cookies/session,webstorage and you can retrieve it anywhere you want.
Hy
Thanks for your code.
Currently I add one new extra filed “Code” in Woocommerce checkout page.
Can I unable to auto fill this filed same as name and billing address filed?
Thanks this works great for putting data saved in Woocommerce profile fields. Not having any luck getting data from custom meta keys using UPME plugin. Any ideas?
I’m not sure how this will render in your comments, so apologies if it’s not formatted but you could just return each one dynamically rather than explicitly returning each one by doing this:
add_filter('woocommerce_checkout_get_value', function($input, $key) {
global $current_user;
// Check if the current user exists
if ($current_user) {
// Return the user property if it exists, false otherwise
return ($current_user->has($key)
? $current_user->$key
: false
);
}
// Return no value
return false;
}, 10, 2);
This just checks the current input/key in the checkout and checks if it exists for that user and returns it, this way you’re not missing any fields by mistake, it ignores guests, and it’s less susceptible to any errors, typos etc.
It seems I should have tested that first, just tested it now and the following works, not sure if I can edit my original answer or not?
add_filter('woocommerce_checkout_get_value', function($input, $key) {
global $current_user;
// Return the user property if it exists, false otherwise
return ($current_user->$key
? $current_user->$key
: false
);
}, 10, 2);
Right, thanks for suggestion. I get the idea, will include snippet in the post for other user too, thanks for sharing.
Only difference is that your snippet checks if exact meta field exists and returns if does, what my snippet does is getting eg ‘first_name’ meta for both ‘shipping_’ and ‘billing_’ first_name.
Reason why I did that is because client requested to have a checkout page without any fields for already registered users.
Idea was to have a ‘quick checkout’ and pre-populate fields from user profile so they don’t need to re-enter same info again.
All of them already had ‘first_name’ field which was populate on ‘profile registration’ but none of them had ‘shipping_’/’billing_’ fields, since registration is mandatory to access website, but shopping is then optional.
Anyhow, thanks once again for sharing.
I’ll include in original post as an example.
Cheers
Ah I see, yeah bringing back `first_name` and `last_name` makes perfect sense for that particular situation for your client.
No worries, glad to have shared it, thanks for including it!
Thanks for sharing, sharing is caring. Cheers :)
Its working for admin side but not on the customer side
Hi there. I need some help.
My clients are registering via “registrationmagic” plugin. In these registration form I can generate special user-meta tags which are saved in the user Account. How can woocommerce pre-populate the billing-adress fields with any meta-data-tag from the user profile?
Registrationmagic generates a meta id called “ust-id”. But I am not familiar with coding and i don’t know what specific meta-tag-path it is saved to.
The ust-id in the checkout form is called “billing_ust”
What do I have to add in the snippet?
case ‘billing_ust’:
return $current_user->ust-id;
break;
I have a question, on woocommerce when i have Shipp to a different address and when i click that the fields are automatically filled with the same info on shipping and billing.
Is there any way i can have it only to fill the top part which is the billing.
Hi me,
Simply add or remove (not)required fields in the switch statement, eg case shipping_last_name and it will not be populated.
For the full ref of available fields please check on WooCommerce docs page.
I think this should be the one that is defined in your WP settings.
Hi, thanks For the great tutorial.
In above code there is the method to put user information only in the fields.
In checkout page I want to fill the fields with variation description like:
User has selected the product variation and then goes directly to checkout and I want to fill the field in checkout form with the selected product variation description.
How can we pass product data in checkout form field?
Wow, you created this Dec 2016 and May 2022, it’s still helping folks. Thank you! My question is how to get the state to populate. I added another line with billing_state, but it doesn’t seem to work.
By the docs it should work.
Can be many things, maybe there is a plugin or theme affecting your code, try adding a higher priority. Or even there is no value, it’s highly specific.
What about the select fields such as country? How do you prefill them?
Hi, great code here ! Excatly what i was looking for :)
But…how should i use it ? Is only copy/past on a funciton.php is good enough ?
Hy henr, that should be enough yes
Hi, thanks for code, Im wondering what if user is not logged in? Can we still pass values from Contact form 7 to woocommerce checkout fields ?
Hi Suyog,
Not sure how you want to pass it, but I think it should be possible to pass anything into this filter.
I think the WooCommerce user data is saved into cookie along with cart data so it should work.
As Suyon I’m looking to pass value from CF7 to some fields into Woocomerce, but fileds are in theproduct page. Is it possibile with a similar code?
yes you can, on submit you can store CF7 data into cookies/session,webstorage and you can retrieve it anywhere you want.
there are hooks available for CF7.
Thanks for the code……….
As Suyon I’m looking to pass value from CF7 to some fields into Woocomerce, but fileds are in theproduct page. Is it possibile with a similar code?
thanks for this code i was looking for this but it doesn’t work on my site can you please figure out for me?
This is great, thanks!
how to get user extra information in this way? like if i created extra field and name if address can i get that value through this code?
I cant find where I should place this code
Hy
Thanks for your code.
Currently I add one new extra filed “Code” in Woocommerce checkout page.
Can I unable to auto fill this filed same as name and billing address filed?
Thanks this works great for putting data saved in Woocommerce profile fields. Not having any luck getting data from custom meta keys using UPME plugin. Any ideas?
This is Great! thank you.
For some reason, it does not pass the phone number. do you have any idea why?
Thanks again!
Not sure, maybe doesn’t pass some validation from WooCommerce.
Phone number is Meta User Data, not part of current data
Might be right, thanks for sharing.
Thanks for your reply
Thanks man, much appreciated.
Glad you found it helpful
I’m not sure how this will render in your comments, so apologies if it’s not formatted but you could just return each one dynamically rather than explicitly returning each one by doing this:
add_filter('woocommerce_checkout_get_value', function($input, $key) {
global $current_user;
// Check if the current user exists
if ($current_user) {
// Return the user property if it exists, false otherwise
return ($current_user->has($key)
? $current_user->$key
: false
);
}
// Return no value
return false;
}, 10, 2);
This just checks the current input/key in the checkout and checks if it exists for that user and returns it, this way you’re not missing any fields by mistake, it ignores guests, and it’s less susceptible to any errors, typos etc.
It seems I should have tested that first, just tested it now and the following works, not sure if I can edit my original answer or not?
add_filter('woocommerce_checkout_get_value', function($input, $key) {
global $current_user;
// Return the user property if it exists, false otherwise
return ($current_user->$key
? $current_user->$key
: false
);
}, 10, 2);
Right, thanks for suggestion. I get the idea, will include snippet in the post for other user too, thanks for sharing.
Only difference is that your snippet checks if exact meta field exists and returns if does, what my snippet does is getting eg ‘first_name’ meta for both ‘shipping_’ and ‘billing_’ first_name.
Reason why I did that is because client requested to have a checkout page without any fields for already registered users.
Idea was to have a ‘quick checkout’ and pre-populate fields from user profile so they don’t need to re-enter same info again.
All of them already had ‘first_name’ field which was populate on ‘profile registration’ but none of them had ‘shipping_’/’billing_’ fields, since registration is mandatory to access website, but shopping is then optional.
Anyhow, thanks once again for sharing.
I’ll include in original post as an example.
Cheers
Ah I see, yeah bringing back `first_name` and `last_name` makes perfect sense for that particular situation for your client.
No worries, glad to have shared it, thanks for including it!
Thanks for sharing, sharing is caring. Cheers :)
Its working for admin side but not on the customer side
Hi there. I need some help.
My clients are registering via “registrationmagic” plugin. In these registration form I can generate special user-meta tags which are saved in the user Account. How can woocommerce pre-populate the billing-adress fields with any meta-data-tag from the user profile?
Registrationmagic generates a meta id called “ust-id”. But I am not familiar with coding and i don’t know what specific meta-tag-path it is saved to.
The ust-id in the checkout form is called “billing_ust”
What do I have to add in the snippet?
case ‘billing_ust’:
return $current_user->ust-id;
break;
You guys would save my life. Thank you very much
Hi Gerwin,
I would say that you should use get_user_meta function in that case.
But I’m not familiar with the plugin you are using so don’t take it for granted.
WOW it worked
Thank you 1000 Times i was looking for that
I have a question, on woocommerce when i have Shipp to a different address and when i click that the fields are automatically filled with the same info on shipping and billing.
Is there any way i can have it only to fill the top part which is the billing.
Thank you
Hi me,
Simply add or remove (not)required fields in the switch statement, eg case
shipping_last_name
and it will not be populated.For the full ref of available fields please check on WooCommerce docs page.
Great, thank you ! ;)
Loving this as I just had this issue with an existing client of my website. I assumed that it already did auto fill.
I tried the initial code in this thread in the functions.php but it came back with an error. Where exactly should this code be placed?
Should go in functions.php, not sure what error you got though.
it’s working on the admin side but not on the customer side
Worked beautifully! Thanks Vlado
How can I add that the country still is ‘DE’ for a non-user?
I wanna combine those line of codes:
add_filter(‘woocommerce_checkout_get_value’, function($input, $key) {
global $current_user;
// Return the user property if it exists, false otherwise
return ($current_user->$key
? $current_user->$key
: false
);
}, 10, 2);
add_filter( ‘default_checkout_country’, ‘change_default_checkout_country’ );
function change_default_checkout_country() {
return ‘DE’; // country code
}
How can I add default country for non-users?
I think this should be the one that is defined in your WP settings.
Hi, thanks For the great tutorial.
In above code there is the method to put user information only in the fields.
In checkout page I want to fill the fields with variation description like:
User has selected the product variation and then goes directly to checkout and I want to fill the field in checkout form with the selected product variation description.
How can we pass product data in checkout form field?
Thanks In Advance
I would guess you need to access data from WC_Cart, please refer to documentation: https://woocommerce.github.io/code-reference/classes/WC-Cart.html
Wow, you created this Dec 2016 and May 2022, it’s still helping folks. Thank you! My question is how to get the state to populate. I added another line with billing_state, but it doesn’t seem to work.
By the docs it should work.
Can be many things, maybe there is a plugin or theme affecting your code, try adding a higher priority. Or even there is no value, it’s highly specific.
What about the select fields such as country? How do you prefill them?
This script worked fine except for the phone number that is not getting populated.
any ideas how to rectify this?
Thank you
Check field names here, could be not correct field name.