Home / Widgets / WooCommerce Checkout Fields Auto-Populate
Duplicate Snippet

Embed Snippet on Your Site

WooCommerce Checkout Fields Auto-Populate

Auto populate checkout fields with users data

<10
Code Preview
php
<?php
/**
 * Unhook Checkout Autocomplete
 */
add_filter( 'woocommerce_checkout_get_value', 'bks_remove_values', 10, 2 );
function bks_remove_values( $value, $input ) {
	$item_to_set_null = array(
		'billing_first_name',
		'billing_last_name',
		'billing_company',
		'billing_address_1',
		'billing_address_2',
		'billing_city',
		'billing_postcode',
		'billing_country',
		'billing_state',
		'billing_email',
		'billing_phone',
		'shipping_first_name',
		'shipping_last_name',
		'shipping_company',
		'shipping_address_1',
		'shipping_address_2',
		'shipping_city',
		'shipping_postcode',
		'shipping_country',
		'shipping_state',
	); // All the fields in this array will be set as empty string, add or remove as required.
	if ( in_array( $input, $item_to_set_null ) ) {
		$value = '';
	}
	return $value;
}
/**
 * Pre-populate Woocommerce checkout fields
 */
add_filter( 'woocommerce_checkout_get_value', function ( $input, $key ) {
	global $current_user;
	switch ( $key ) :
		case 'billing_first_name':
		case 'shipping_first_name':
			return $current_user->first_name;
			break;
		case 'billing_last_name':
		case 'shipping_last_name':
			return $current_user->last_name;
			break;
		case 'billing_email':
			return $current_user->user_email;
			break;
		case 'billing_phone':
			return $current_user->phone;
			break;
		case 'field_1':
			return $current_user->field_1;
			break;
	endswitch;
}, 10, 2 );

Comments

Add a Comment