WooCommerce: Update Order Field Value After a Successful Order

We’ve already seen how to update user meta after a successful order, but this time our goal is to “correct” or “edit” a checkout field value after the order is placed.

You could for example add a phone number prefix if it’s not there, and by doing so, correct the phone number before sending it to your courier. Likewise, you could remove punctuation, trim spaces, format accents, and do any manipulation you desire on whatever order field.

So, here’s how they do it. Enjoy!

Here’s a test order where phone hasn’t been entered with the Italian prefix (+39). The snippet below will alter this field upon checkout, and this screen should display the phone number together with the country code.

PHP Snippet: Alter Order Field Value After An Order is Placed @ WooCommerce Checkout

In this example, we’ll see how to re-format the phone number entered during checkout, by adding a prefix if it’s not there. Also, you’ll learn about the handy get_country_calling_code() WooCommerce function, which is good to know!

/**
 * @snippet       Update Order Meta After a Successful Order - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 4.6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_thankyou', 'bbloomer_alter_checkout_fields_after_order' );

function bbloomer_alter_checkout_fields_after_order( $order_id ) {
	$order = wc_get_order( $order_id );
	$phone = $order->get_billing_phone();
	$calling_code = WC()->countries->get_country_calling_code( $order->get_billing_country() );
	$calling_code = is_array( $calling_code ) ? $calling_code[0] : $calling_code;
	if ( $phone && $calling_code && ! str_starts_with( $phone, $calling_code ) ) {
		// str_starts_with() works on PHP 8+ only
		$phone = $calling_code . $phone;
		update_post_meta( $order_id, '_billing_phone', $phone );	
	}	
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). 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.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

4 thoughts on “WooCommerce: Update Order Field Value After a Successful Order

  1. Ho, I have not implemented this since WordPress does not support PHP 8 yet, but just from the code reading it looks like it does not work correctly. The phone number format without country prefix always starts with 0 (like 0912345678) and the international version of it leaves the zero out (like +421912345678). The php code above simply adds the local format to prefix and the number becomes incorrect: +4210912345678. Please correct me if I am wrong.

    1. Honestly I don’t remember, it’s been a long time since I wrote this snippet. Have you tested it already?

  2. Thanks Rodolfo, awesome stuff!

    I like this code for writing a value to a native WC order field:
    update_post_meta( $order_id, ‘_billing_phone’, $phone );

    How can I write to an ACF Field? I have a boolean field ‘is_backorder’, I have my code that finds if the stock of any product is < 1, but I cannot for the life of it write anything to this custom field… Do you have any experience with ACF?

    Help is much appreciated!
    Daniel

    1. Hi Daniel, 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!

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

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 :)

Your email address will not be published.