Skip to content

How To prefill WooCommerce Checkout Page in WordPress Programmatically.

Woocommerce Checkout Page in Wordpress

Ever thought of Prefill woocommerce checkout page with customer details so customers don’t have to fill the form again and again.

Say thanks to WordPress filters that make it possible to Pre-populate Woocommerce Checkout fields. you can prefill or pre populate woocommerce checkout page via some attributes.

Announcement: Checkout our Personal recommended list of Tools and Websites we personally use.

the one that I am using is the default value. in the code below am getting data from user meta and setting that as a default values to billing fields.

Note: This will work when user is already logged in and going to order for the first time.

Let’s Pre-populate Checkout Page with User Data in Woocommerce.

All you need to do is to paste this code in your theme’s functions.php. assuming that you are already using a child theme.

add_filter( 'woocommerce_checkout_fields', 'itdoc_remove_fields', 9999 );


function itdoc_remove_fields( $woo_checkout_fields_array ) {
    
    $user = wp_get_current_user();
  
       $dealer_name= get_user_meta($user->ID, 'dealer_name' , true);
	$dealer_phone= get_user_meta($user->ID, 'Delaer_phone' , true);
	$dealer_company= get_user_meta($user->ID, 'dealer_company' , true);
	$dealer_address= get_user_meta($user->ID, 'dealer_address' , true);
	$dealer_city= get_user_meta($user->ID, 'dealer_city' , true);
	$dealer_state= get_user_meta($user->ID, 'dealer_state' , true);
	

         // removing billing last name and post code filds.
	     unset( $woo_checkout_fields_array['billing']['billing_last_name'] );
	     unset( $woo_checkout_fields_array['billing']['billing_postcode'] );

	 
	  $woo_checkout_fields_array['billing']['billing_first_name']['default'] = $dealer_name ;
	  $woo_checkout_fields_array['billing']['billing_phone']['default'] = $dealer_phone ;
	  $woo_checkout_fields_array['billing']['billing_company']['default'] = $dealer_company ;
	  $woo_checkout_fields_array['billing']['billing_phone']['default'] = $dealer_phone ;
	  $woo_checkout_fields_array['billing']['billing_address_1']['default'] = $dealer_address ;
          $woo_checkout_fields_array['billing']['billing_city']['default'] = $dealer_city;
	  $woo_checkout_fields_array['billing']['billing_state']['default'] = $dealer_state;



	return $woo_checkout_fields_array;
}

Feel free to ask me anything if you face some issues implementing this. because there can be various scenarios where you can use different WordPress filters.

basically the code above uses wordpress get_user_meta function to fetch the user meta profile fields.

if the code works for you please let us know via comments. Thanks!

Scan the code