For a special group of customers that aren't yet WP user in my system, I am directing them to a special page, where they'll choose from a limited set of products. I already have all of their information and I it's going to pre-populate on this landing page. When they've verified their information, it will add their product to the cart and skip straight to the checkout. I've got all that down so far.
What I want to do is pre-populate the checkout data with the customer name and billing information that I have and I'm not entirely certain how to do that. But here's what I've got so far:
function onboarding_update_fields( $fields = array() ) {
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
if( 'testtoken' == $token ) {
$fields['billing']['billing_first_name']['value'] = 'Joe';
var_dump( $fields );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );
How do I change the value of the checkout fields? The code above doesn't do it. Point me in the right direction on one, and I can do the rest.
I looked here but didn't quite find what I was looking for either.
Thanks!
Filters allow you to modify information, but you must return that information from your function.
So, in this case, you're simply missing a return $fields;
in your function:
function onboarding_update_fields( $fields = array() ) {
// check if it's set to prevent notices being thrown
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
// yoda-style to prevent accidental assignment
if( 'testtoken' == $token ) {
// if you are having issues, it's useful to do this below:
var_dump( $fields );
// remove the var_dump once you've got things working
// if all you want to change is the value, then assign ONLY the value
$fields['billing']['billing_first_name']['value'] = 'Joe';
// the way you were doing it before was removing core / required parts of the array - do not do it this way.
// $fields['billing']['billing_first_name']['value'] = array( 'value' => 'Joe');
}
// you must return the fields array
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );
Update:
After seeing for some reason the above doesn't work, I sniffed some code on another plugin, and they way they do it (and it clearly works) is like so:
function onboarding_update_fields( $fields = array() ) {
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
if( 'testtoken' == $token ) {
// Assign the value to the $_POST superglobal
$_POST['billing_first_name'] = 'Joe';
}
return $fields;
}
So - to be positive that this didn't overwrite / stomp user-entered information, I might suggest considering doing it something like this (of course test to be sure it works):
function onboarding_update_fields( $fields = array() ) {
$token = ( ! empty( $_GET['token'] ) ) ? $_GET['token'] : '';
if( 'testtoken' == $token ) {
// Assign the value to the $_POST superglobal ONLY if not already set
if ( empty( $POST['billing_first_name'] ) ) {
$_POST['billing_first_name'] = 'Joe';
}
}
return $fields;
}