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        Get CustomizeWoo.com FREE
 * @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' => __('https://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 custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]
  • WooCommerce: Disable Payment Gateway by Country
    You might want to disable PayPal for non-local customers or enable a specific gateway for only one country… Either way, this is a very common requirement for all of those who trade internationally. Here’s a simple snippet you can further customize to achieve your objective. Simply pick the payment gateway “slug” you want to disable/enable […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

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

  1. Thanks for snippet Rodolfo, works perfect!

    I’ve changed type “text” for “radio” but the value is not save:

    add_action( 'woocommerce_after_checkout_billing_form', 'jrc_add_user_field_to_checkout' );
    
    function jrc_add_user_field_to_checkout( $checkout ) { 
    	$current_user = wp_get_current_user();
    	$saved_rrss = $current_user->rrss;
    
    	woocommerce_form_field('rrss', array(
    			'type' => 'radio',        
    			'class' => array('rrss form-row-wide'),        
    			'label' => __('RR SS', 'jcode'),        
    			'options' => array(
    					1 => __( 'Facebook', 'jcode' ),
    					2 => __( 'Instagram', 'jcode' ),
    					3 => __( 'Otro', 'jcode' ),
    				),
    			'default' => '1',
    			'required' => false
    		),
    	$saved_rrss ); 
    	
    }
    
    add_action( 'woocommerce_checkout_update_user_meta', 'jrc_checkout_field_update_user_meta' );
     
    function jrc_checkout_field_update_user_meta( $user_id ) { 
    	if ( $user_id && $_POST['rrss'] ) {
    	// once again, use "rrss"		
    	   $args = array(
    					'ID' => $user_id,
    					'rrss' => esc_attr( $_POST['rrss'] )
    			);      
    		   
    		wp_update_user( $args );
    	}
    }
    

    Any ideas?

    I am interested in Customize Woo, will I learn in the course to include fields anywhere in Woocommece??

    Thank you so much.

    1. Hello Jesus, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      (CustomizeWoo is about overall WooCommerce customization so this would be very specific. However, you get unlimited support from me and can share code, so I would be able to help you)

  2. What if i want to save the changes to all of the fields made from the checkout to the user profile, do i have to list them all in the ‘bbloomer_checkout_field_update_user_meta’?
    For ex if i change the name/last name/adress/phone in the checkout page they are not reflected in the user profile.

    1. Uhm there is something strange then. Disable all plugins but Woo and try again

  3. Useful snippet. But is it possible to retrieve the user meta from the WordPress user profile to a custom checkout field ( just reverse of the technique you described)?

    1. Hi Sheikh, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. 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 ๐Ÿ™‚

  5. 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 ๐Ÿ™‚

  6. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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