Home » WooCommerce: Populate Checkout Fields From URL

WooCommerce: Populate Checkout Fields From URL

by Tutor Aspire

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=Tutoraspire&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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'woocommerce_add_cart_item_data', 'tutoraspire_save_custom_data_in_cart_object', 9999, 3 );

function tutoraspire_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' , 'tutoraspire_populate_checkout', 9999 );
  
function tutoraspire_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;
}

You may also like