Skip to content

Instantly share code, notes, and snippets.

@himanshuahuja96
Forked from Bobz-zg/woo-checkout.php
Created February 13, 2020 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himanshuahuja96/fd65adc6849012c3f9d8d01721e03338 to your computer and use it in GitHub Desktop.
Save himanshuahuja96/fd65adc6849012c3f9d8d01721e03338 to your computer and use it in GitHub Desktop.
Pre-populate Woocommerce checkout fields
<?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