Skip to content

Instantly share code, notes, and snippets.

@Bobz-zg
Last active April 12, 2023 01:41
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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);
@rubiwane
Copy link

rubiwane commented Feb 6, 2017

Hi,
Do you think you could populates from gravity forms to the woo commercer Checkout?

@vvp242424
Copy link

@rubiwane I was able to do that with a sessions plugin I created then instead of the return $current_use->field; I put return $_SESSION['what your passing'];

This required me to create a plugin with some hooks from gravity forms like gform_after_submission. https://www.gravityhelp.com/documentation/article/gform_after_submission/

If anyone things that is not a good idea let me know. the session is destroyed at login and logout.

@eganet2002
Copy link

Hi, vvp242424 could you let me know how you achieved that? Do you still have the plugin you creatd?

@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