WooCommerce: Update User Meta After a Successful Order

When an order is placed in WooCommerce, you might want to change/add something in the User Meta programmatically.

For example, you could “check” a custom checkbox in the User Profile. Or maybe assign the User Twitter username. And so on ๐Ÿ™‚

WooCommerce: edit user meta after successful checkout

PHP Snippet: Update User Meta After a Successful Order in WooCommerce


/**
 * @snippet       Update User Meta After a Successful Order - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=21955
 * @author        Rodolfo Melogli
 * @compatible    WC 3.6.4
 */

add_action( 'woocommerce_thankyou', 'bbloomer_checkout_save_user_meta');

function bbloomer_checkout_save_user_meta( $order_id ) {
	
	$order = wc_get_order( $order_id );
	$user_id = $order->get_user_id();
	
	if ( $order->get_total() > 100 ) {  // Define your condition here
		update_user_meta( $user_id, 'custom_checkbox', 'on');
	}

}

Where to add this snippet?

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 files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

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 Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

8 thoughts on “WooCommerce: Update User Meta After a Successful Order

  1. Hi Rodolfo! I follow your blog for WooCommerce. It’s amazing your work.
    Just update the code for the woo-commerce order. I use

    $order = wc_get_order( $order_id );
    1. Thank you!

  2. Hi,

    Im not a developer.

    But how can i update simply the order_id in the user data? (Because it is now in the post meta i think).

    Tx in advance!

    1. Hey Ben – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  3. great with all your articles
    can you pls tell me how to update a custom type after the order is complete.
    i use:

    
    add_action( 'user_register', 'wpse_216921_woocommerce_orders', 10, 1 );
    function wpse_216921_woocommerce_orders( $user_id )
    {
        // Get user info
        $user_info = get_userdata( $user_id );
    
        // Create a new post
        $user_post = array(
            'post_title'  => 'WooCommerce' . ' - ' . $user_info->user_email,
            'post_type'   => 'orders',
            'post_status' => 'publish',
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    
        // Add custom order info as custom fields
        add_post_meta( $post_id, 'firstname', $user_info->user_firstname );
        add_post_meta( $post_id, 'lastname', $user_info->user_lastname );
        add_post_meta( $post_id, 'email', $user_info->user_email );
        add_post_meta( $post_id, 'member', $user_info->ID );
        add_post_meta( $post_id, 'status', 'ERR' );
    }
    

    o create a custom post type after user register, i want after order is complete to have:

        add_post_meta( $post_id, 'status', 'OK' ); 

    can you pls help me? also min my code first name and last name is not working using woocomerce register (only with normal register) have any glue why?
    thanks

    1. Marius, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  4. Love your site Rodolfo!
    I have used several of your code snippets with great success. One change I made with this one (see below) was prompted by a deprecated code warning from woocommerce regarding accessing user_ids. Thanks again for all your tireless work for helping use neophytes learn how to customize woocommerce.

     
    $order = new WC_Order( $order_id );
    // $user_id = $order->user_id;
    $user_id = $order->get_user_id();
     
    1. Fantastic, thanks so much for your help ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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