Posted on

WooCommerce: Display & Save WP User Field (e.g. user_url) @ Checkout

I’m curious to know how many had the same problem. At WooCommerce checkout, some user fields such as billing_name, shipping_address_1, etc. are automatically saved into the “WordPress User Profile” upon processing.

But what if we also wanted to display and save another existing user field, such as “user_twitter”, or “user_url”, which you can find in the WP User Profile by default? Well, this is very easy: first, we add a custom checkout field; then, we make sure that when the checkout is processed we save that field correctly!

Display & save a default WordPress User Field via WooCommerce Checkout

PHP Snippet: Show & Save WordPress User Field Upon WooCommerce Checkout


/**
 * @snippet       Display & Save WP User Field @ Checkout - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=21737
 * @author        Rodolfo Melogli
 * @compatible    WC 3.5.1
 */


// ------------------------
// 1. Display field @ Checkout

add_action( 'woocommerce_after_checkout_billing_form', 'bbloomer_add_user_field_to_checkout' );
 
function bbloomer_add_user_field_to_checkout( $checkout ) {

$current_user = wp_get_current_user();
$saved_url = $current_user->user_url;

woocommerce_form_field( 'user_url', array(        
'type' => 'text',        
'class' => array('user_url form-row-wide'),        
'label' => __('Website URL'),        
'placeholder' => __('http://yoursite.com'),        
'required' => false
), 
$saved_url ); 
 
}
 

// ------------------------
// 2. Save Field Into User Meta
 
add_action( 'woocommerce_checkout_update_user_meta', 'bbloomer_checkout_field_update_user_meta' );
 
function bbloomer_checkout_field_update_user_meta( $user_id ) { 

if ( $user_id && $_POST['user_url'] ) {

// once again, use "user_url"
	
	$args = array(
                'ID' => $user_id,
                'user_url' => esc_attr( $_POST['user_url'] )
        );      
      
    wp_update_user( $args );
}

}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

6 thoughts on “WooCommerce: Display & Save WP User Field (e.g. user_url) @ Checkout

  1. I replaced all instances of user_url with bob and it didn’t save anything to the database. What am I doing wrong?

    1. Hello Chris, thanks for your comment! I’ve revised the snippet – let me know if this version works ๐Ÿ™‚

  2. Hi

    Thanks for sharing but I think it won’t work with dropdown fields right?

    1. Hey Robert – thanks so much for your comment! The “woocommerce_form_field” function can also generate a dropdown, so you can definitely save that data as well ๐Ÿ™‚

  3. Thank you for the useful snippet – awesome stuff!

    FYI – there’s an “}” under the “// 1. Display field @ Checkout”-section.

    1. Thank you so much Kasper ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.