Skip to main content

Pre-populate Woocommerce checkout fields

Pre-populate Woocommerce checkout fields for logged in user.
This will populate ‘value’ attribute of checkout input field.

39 comments

  1. Hi, great code here ! Excatly what i was looking for :)

    But…how should i use it ? Is only copy/past on a funciton.php is good enough ?

  2. Hi, thanks for code, Im wondering what if user is not logged in? Can we still pass values from Contact form 7 to woocommerce checkout fields ?

    1. Hi Suyog,

      Not sure how you want to pass it, but I think it should be possible to pass anything into this filter.
      I think the WooCommerce user data is saved into cookie along with cart data so it should work.

    2. As Suyon I’m looking to pass value from CF7 to some fields into Woocomerce, but fileds are in theproduct page. Is it possibile with a similar code?

  3. As Suyon I’m looking to pass value from CF7 to some fields into Woocomerce, but fileds are in theproduct page. Is it possibile with a similar code?

  4. how to get user extra information in this way? like if i created extra field and name if address can i get that value through this code?

  5. Hy
    Thanks for your code.
    Currently I add one new extra filed “Code” in Woocommerce checkout page.
    Can I unable to auto fill this filed same as name and billing address filed?

  6. Thanks this works great for putting data saved in Woocommerce profile fields. Not having any luck getting data from custom meta keys using UPME plugin. Any ideas?

  7. I’m not sure how this will render in your comments, so apologies if it’s not formatted but you could just return each one dynamically rather than explicitly returning each one by doing this:

    add_filter('woocommerce_checkout_get_value', function($input, $key) {
    global $current_user;
    // Check if the current user exists
    if ($current_user) {
    // Return the user property if it exists, false otherwise
    return ($current_user->has($key)
    ? $current_user->$key
    : false
    );
    }
    // Return no value
    return false;
    }, 10, 2);

    This just checks the current input/key in the checkout and checks if it exists for that user and returns it, this way you’re not missing any fields by mistake, it ignores guests, and it’s less susceptible to any errors, typos etc.

    1. It seems I should have tested that first, just tested it now and the following works, not sure if I can edit my original answer or not?

      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);

    2. Right, thanks for suggestion. I get the idea, will include snippet in the post for other user too, thanks for sharing.

      Only difference is that your snippet checks if exact meta field exists and returns if does, what my snippet does is getting eg ‘first_name’ meta for both ‘shipping_’ and ‘billing_’ first_name.

      Reason why I did that is because client requested to have a checkout page without any fields for already registered users.
      Idea was to have a ‘quick checkout’ and pre-populate fields from user profile so they don’t need to re-enter same info again.

      All of them already had ‘first_name’ field which was populate on ‘profile registration’ but none of them had ‘shipping_’/’billing_’ fields, since registration is mandatory to access website, but shopping is then optional.

      Anyhow, thanks once again for sharing.
      I’ll include in original post as an example.

      Cheers

    3. Ah I see, yeah bringing back `first_name` and `last_name` makes perfect sense for that particular situation for your client.

      No worries, glad to have shared it, thanks for including it!

  8. Hi there. I need some help.
    My clients are registering via “registrationmagic” plugin. In these registration form I can generate special user-meta tags which are saved in the user Account. How can woocommerce pre-populate the billing-adress fields with any meta-data-tag from the user profile?
    Registrationmagic generates a meta id called “ust-id”. But I am not familiar with coding and i don’t know what specific meta-tag-path it is saved to.
    The ust-id in the checkout form is called “billing_ust”
    What do I have to add in the snippet?

    case ‘billing_ust’:
    return $current_user->ust-id;
    break;

    You guys would save my life. Thank you very much

  9. I have a question, on woocommerce when i have Shipp to a different address and when i click that the fields are automatically filled with the same info on shipping and billing.

    Is there any way i can have it only to fill the top part which is the billing.

    Thank you

    1. Hi me,
      Simply add or remove (not)required fields in the switch statement, eg case shipping_last_name and it will not be populated.
      For the full ref of available fields please check on WooCommerce docs page.

  10. Loving this as I just had this issue with an existing client of my website. I assumed that it already did auto fill.

    I tried the initial code in this thread in the functions.php but it came back with an error. Where exactly should this code be placed?

  11. How can I add that the country still is ‘DE’ for a non-user?

    I wanna combine those line of codes:

    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);

    add_filter( ‘default_checkout_country’, ‘change_default_checkout_country’ );

    function change_default_checkout_country() {
    return ‘DE’; // country code
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Please don’t paste any HTML, Js or PHP code into comments box, use pastebin.com or similar service to share code examples.

Characters left: 1000