Last active
April 12, 2023 01:41
-
-
Save Bobz-zg/1d59f0835678c3787597121255a959d3 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); |
Sadly, doing absolutely nothing. I am creating a order and populating the cart and want to set the check out values to a user's already created information.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would it be possible to pre-populate a WooCommerce bookings bookable product name, price, & duration?