WooCommerce: Populate Checkout Fields From URL

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!

With a single URL, and the PHP snippet below, I was able to (1) add to cart and (2) populate WooCommerce Checkout Billing and Shipping fields thanks to query strings

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[email protected]&bph=123456789&bco=BusinessBloomer&bcy=IT&bst=PA&ba1=viaRoma&ba2=1&bci=Palermo&bpo=90100&sfn=John&sln=Doe&sem[email protected]&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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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;
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

2 thoughts on “WooCommerce: Populate Checkout Fields From URL

  1. 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.

    1. Perfect, thanks for that!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.