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: Update User Meta After a Successful Order”
JARED
Thank you for the concept of this article. It may help me accomplish my goal.
I would like to create a snippet that updates a user meta field with the most recent products purchased on a woocommerce order as a basic list separated by commas. Is this possible? I think I know I’ll need the following.
‘meta_field_name’, $order->get_items()
But I don’t know how to make the list of items to appear in the ACF meta field (which is a text field).
Hi Jared, first of all, I would not use “get_items” as you save an array of objects, where each object contains a lot of information. It would be easier to store product IDs only, so you could loop through “get_items” and get the IDs only. As per the actual functionality, 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!
Hi, thanks for the great snippet.
A hopefully quick question:
I’m trying to associate a job listing created during the order process with the woocommerce user. The job listing id is in the order meta as _job_listing and I want to use your code above to link it to the user.
My one concern is that a user could have multiple job listings associated with their account. Will the code above replace existing ones or simply append this new job listing id to existing ones in the user meta?
Hi Paul, as long as I know you can pass an array as meta value, which means you can “get” the existing meta value and concatenate an additional array value to it. Hope this helps
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
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
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
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.
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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you!
Thank you for the concept of this article. It may help me accomplish my goal.
I would like to create a snippet that updates a user meta field with the most recent products purchased on a woocommerce order as a basic list separated by commas. Is this possible? I think I know I’ll need the following.
‘meta_field_name’, $order->get_items()
But I don’t know how to make the list of items to appear in the ACF meta field (which is a text field).
Can you suggest what I’d need to do?
Hi Jared, first of all, I would not use “get_items” as you save an array of objects, where each object contains a lot of information. It would be easier to store product IDs only, so you could loop through “get_items” and get the IDs only. As per the actual functionality, 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!
Hi, thanks for the great snippet.
A hopefully quick question:
I’m trying to associate a job listing created during the order process with the woocommerce user. The job listing id is in the order meta as _job_listing and I want to use your code above to link it to the user.
My one concern is that a user could have multiple job listings associated with their account. Will the code above replace existing ones or simply append this new job listing id to existing ones in the user meta?
Hi Paul, as long as I know you can pass an array as meta value, which means you can “get” the existing meta value and concatenate an additional array value to it. Hope this helps
Hi Rodolfo! I follow your blog for WooCommerce. It’s amazing your work.
Just update the code for the woo-commerce order. I use
Thank you!
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!
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
great with all your articles
can you pls tell me how to update a custom type after the order is complete.
i use:
o create a custom post type after user register, i want after order is complete to have:
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
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
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.
Fantastic, thanks so much for your help ๐