Skip to content

Instantly share code, notes, and snippets.

@Bobz-zg
Last active April 12, 2023 01:41
Show Gist options
  • Save Bobz-zg/1d59f0835678c3787597121255a959d3 to your computer and use it in GitHub Desktop.
Save Bobz-zg/1d59f0835678c3787597121255a959d3 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);
@DHergott91
Copy link

Would it be possible to pre-populate a WooCommerce bookings bookable product name, price, & duration?

@dskurth
Copy link

dskurth commented May 13, 2019

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