On top of adding products to cart via URL and redirect to checkout, there is a way to also fill out the Checkout page input fields within the same link.
This could be super handy when you know the billing/shipping details of a registered or guest customer and want to speed up the order process.
It’s important to note that the URL will need to contain personal data e.g. email address, billing address, phone number, and so on; you need to make sure the URL is only shared with the specific customer (in an email, for example, as content is tailored to the subscriber; or only when the WooCommerce customer is logged in if you’re using the URL behind a website button).
Once that’s clear, let’s go ahead, and let’s see how my WooCommerce snippet works. Enjoy!
PHP Snippet: Populate WooCommerce Checkout Billing & Shipping Via URL
Notes:
1) Once the snippet is installed, you can use the following URL format (in bold you see the URL parameters you must use, and they need to be identical to the ones defined in the snippet):
example.com/checkout/?add-to-cart=1234&bfn=Rodolfo&bln=Melogli&bem=test@test.com&bph=123456789&bco=BusinessBloomer&bcy=IT&bst=PA&ba1=viaRoma&ba2=1&bci=Palermo&bpo=90100&sfn=John&sln=Doe&sem=john@doe.doe&sco=JDCO&scy=US&sst=CA&sa1=SunsetBoulevard&sa2=1234&sci=LosAngeles&spo=90145
2) No blank spaces are allowed in the URL e.g. “SunsetBoulevard”. If you need to generate a space e.g. “Sunset Boulevard” you need to use the encoded space character: “%20”. Untested.
3) You can omit whatever you wish in the URL, except the add to cart product ID, which is responsible for adding a product to Cart. Read this helpful guide to add simple / variable / etc. products to cart with a given quantity
4) The URL redirects to “/checkout/” – that’s the Checkout page slug. If you have edited that, change the URL accordingly
5) Countries and – in some cases – states require their 2-letter codes (“US”, “CA” for example)
6) Inside the PHP snippet, be very careful when editing the $topopulate array. Array values must match the WooCommerce checkout field IDs, otherwise it won’t populate them. You can remove any key/value from the array if you don’t need to populate whatever field e.g. Billing Phone
/**
* @snippet Populate Fields Via URL @ WooCommerce Checkout
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 6
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_save_custom_data_in_cart_object', 9999, 3 );
function bbloomer_save_custom_data_in_cart_object( $cart_item_data, $product_id, $variation_id ) {
$topopulate = array(
'bfn' => 'billing_first_name',
'bln' => 'billing_last_name',
'bem' => 'billing_email',
'bph' => 'billing_phone',
'bco' => 'billing_company',
'bcy' => 'billing_country',
'bst' => 'billing_state',
'ba1' => 'billing_address_1',
'ba2' => 'billing_address_2',
'bci' => 'billing_city',
'bpo' => 'billing_postcode',
'sfn' => 'shipping_first_name',
'sln' => 'shipping_last_name',
'sem' => 'shipping_email',
'sco' => 'shipping_company',
'scy' => 'shipping_country',
'sst' => 'shipping_state',
'sa1' => 'shipping_address_1',
'sa2' => 'shipping_address_2',
'sci' => 'shipping_city',
'spo' => 'shipping_postcode',
);
foreach ( $topopulate as $urlparam => $checkout_field ) {
if ( isset( $_GET[$urlparam] ) && ! empty( $_GET[$urlparam] ) ) {
$cart_item_data[$checkout_field] = esc_attr( $_GET[$urlparam] );
}
}
return $cart_item_data;
}
add_filter( 'woocommerce_checkout_fields' , 'bbloomer_populate_checkout', 9999 );
function bbloomer_populate_checkout( $fields ) {
$topopulate = array(
'bfn' => 'billing_first_name',
'bln' => 'billing_last_name',
'bem' => 'billing_email',
'bph' => 'billing_phone',
'bco' => 'billing_company',
'bcy' => 'billing_country',
'bst' => 'billing_state',
'ba1' => 'billing_address_1',
'ba2' => 'billing_address_2',
'bci' => 'billing_city',
'bpo' => 'billing_postcode',
'sfn' => 'shipping_first_name',
'sln' => 'shipping_last_name',
'sem' => 'shipping_email',
'sco' => 'shipping_company',
'scy' => 'shipping_country',
'sst' => 'shipping_state',
'sa1' => 'shipping_address_1',
'sa2' => 'shipping_address_2',
'sci' => 'shipping_city',
'spo' => 'shipping_postcode',
);
foreach ( WC()->cart->get_cart() as $cart_item ) {
foreach ( $topopulate as $urlparam => $checkout_field ) {
if ( isset( $cart_item[$checkout_field] ) && ! empty( $cart_item[$checkout_field] ) ) {
switch ( substr( $checkout_field, 0, 7 ) ) {
case 'billing':
$fields['billing'][$checkout_field]['default'] = $cart_item[$checkout_field];
break;
case 'shippin':
$fields['shipping'][$checkout_field]['default'] = $cart_item[$checkout_field];
break;
}
}
}
}
return $fields;
}
Love this piece of code. Thanks
Great!
This is really cool, I want to use it in a prototype for a plugin. But can’t get it to work. Should it be able to work in a local environment?
Not using anything specific, my theme is 2024 with a child theme, that has the code in the functions.php.
Thanks in advance
Uhm, are you on the “Checkout Block”?
The PHP Snippet: Populate WooCommerce Checkout Billing & Shipping Via URL works fine except for the phone number which is not populated; I am not sure why, any ideas?
Thank you
That’s really weird. Billing phone, right?
Yes that is correct!
Thanks. Not sure – maybe special characters? Or a custom validation that doesn’t let you enter wrong phone numbers? I don’t really know, because it works ok on my end.
I am having a problem with that it is not populating the billing address. When I hit the checkout URL it adds to quantity of products, but isn’t populating the First name, Last name, Address and Phone. The URL I am using /checkout/?add-to-cart=5653&quantity=1&bfn=Rodolfo&sfn=Rodolfo to test on my site. Also with this i want to populate the Product name with the custom price. But i am not even sure for that how is it possible? Can you please help me with this?
Disha, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet.
To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentytwenty” theme (load the snippet there in functions.php) as explained here: https://www.businessbloomer.com/lesson/trwm4l01/
Once you do that, does it work? If yes, you have a problem with your current theme or one of the plugins.
Hope this helps!
Hi there, Can we create order with URL ? For bank transfoer or COD payment ?
Thanks
Hello Prite, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I can’t figure out why it’s not working on my site. I don’t get an error message. It adds the product to the cart but doesn’t populate any of the billing fields. I added it to the functions.php of the child theme. Cleared the cache and disabled any potentially conflicting plugins. Could it be related to the fact that the checkout page is created by adding the short code to an Elementor page?
Not sure. Can you paste here a sample URL that should populate the checkout?
It’s not working on my site either. There are no other plugins active that could interfere with Woocommerce. I als tried other methods like addfilter and addaction on different hooks: all my checkoutfields stay empty…
Reinier, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet.
To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentytwenty” theme (load the snippet there in functions.php) as explained here: https://www.businessbloomer.com/lesson/trwm4l01/
Once you do that, does it work? If yes, you have a problem with your current theme or one of the plugins.
Hope this helps!
This works brilliantly, thank you!
Is it possible to add a custom product with a custom price directly. ie. the url parameters would have a custom product name with a custom price that creates a product ‘on the fly’ (does not need to be saved as a product for future use) that they can checkout?
The use-case is that we’d like to offer a link to checkout for quotes that we produce offline. The reason for doing this through WooCommerce and not a form with payment link is that we’d like to use Woo plugins for calculating and applying delivery costs and discounts.
Look forward to your thoughts.
Yes, some additional code would be required, but that’s possible. I’d still recommend creating a “hidden” product, and using the URL to change its checkout price programmatically, which is the easiest thing to do. Hope this helps!
Thank you for pointing me in the right direction!
We created a hidden product and were able to get a custom price to show in the checkout.
Awesome!
Great post. I did not know how to do this. I will make a slight change to address the privacy issue that you highlight.
My normal scenario is that I send a subscriber a direct mail with a links to a product and so this is very useful.
I will not put the explicit checkout field data in the URL as you have. I will put a key such as encrypted_subscriber_id in the link. I will then use your code but look-up the details. This removes the privacy issues that you highlight.
Perfect, thanks for that!