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

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]

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. Follow @rmelogli

14 thoughts on “WooCommerce: Populate Checkout Fields From URL

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

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

  2. Hi there, Can we create order with URL ? For bank transfoer or COD payment ?
    Thanks

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

  3. 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?

    1. Not sure. Can you paste here a sample URL that should populate the checkout?

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

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

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

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

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

  5. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *