PHPSnippetsWoocommerce

Pre-fill WooCommerce Checkout Fields

By January 15, 2021No 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;
}
https://www.yoursite.com/checkout-page/[email protected]&fname=John&lname=Smith

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

Contact us to learn about adopting the web for your business

    Author Jarod Thornton

    More posts by Jarod Thornton