-
-
Save himanshuahuja96/fd65adc6849012c3f9d8d01721e03338 to your computer and use it in GitHub Desktop.
Pre-populate Woocommerce checkout fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Pre-populate Woocommerce checkout fields | |
* Note that this filter populates shipping_ and billing_ fields with a different meta field eg 'first_name' | |
*/ | |
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; | |
endswitch; | |
}, 10, 2); | |
/** | |
* Dynamically pre-populate Woocommerce checkout fields with exact named meta field | |
* Eg. field 'shipping_first_name' will check for that exact field and will not fallback to any other field eg 'first_name' | |
* | |
* @author Joe Mottershaw | https://cloudeight.co | |
*/ | |
add_filter('woocommerce_checkout_get_value', function($input, $key) { | |
global $current_user; | |
// Return the user property if it exists, false otherwise | |
return ($current_user->$key | |
? $current_user->$key | |
: false | |
); | |
}, 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment