Skip to main content
SnippetsPHPWoocommerce

Pre-fill WooCommerce Checkout Fields

By January 15, 2021October 5th, 2022No Comments

Like it reads!

// Autofill checkout fields from URL
 
add_filter( 'woocommerce_checkout_fields' , 'prefill_billing_fields' );
 
function prefill_billing_fields ( $address_fields ) {
 
 
   // Get the data from the URL
 
   if ( isset( $_GET['fname'] ) || isset( $_GET['lname'] ) || isset( $_GET['email'] ) ) 
   {
 
   // wp_die();
 
       $fname = isset( $_GET['fname'] ) ? esc_attr( $_GET['fname'] ) : '';
       $lname = isset( $_GET['lname'] ) ? esc_attr( $_GET['lname'] ) : '';
       $em   = isset( $_GET['email'] ) ? esc_attr( $_GET['email'] ) : '';
 
       // First Name
 
      if( isset($_GET['fname']) && ! empty($_GET['fname']) ){
         if( isset( $address_fields['billing']['billing_first_name'] ) ){
             $address_fields['billing']['billing_first_name']['default'] = $fname;
         }
      }
 
      // Last Name
 
      if( isset($_GET['lname']) && ! empty($_GET['lname']) ){
          if( isset( $address_fields['billing']['billing_last_name'] ) ){
             $address_fields['billing']['billing_last_name']['default'] = $lname;
          }
      }
 
      // Email
 
      if( isset($_GET['email']) && ! empty($_GET['email']) ){
          if(isset( $address_fields['billing']['billing_email'] )){
             $address_fields['billing']['billing_email']['default'] = $em;
          }
      }
   }
    
   return $address_fields;
}
e.g. https://www.yoursite.com/checkout-page/?email=name@example.com&fname=John&lname=Smith

Thanks! https://newpulselabs.com/prefill-checkout-fields-cartflows/

Contact us to learn more about Adopt the Web for your business

Author Jarod Thornton

More posts by Jarod Thornton