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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 4.6
 * @community     https://businessbloomer.com/club/
 */

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

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

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

  1. Hi!
    thanks for your code. Could be possible to use your code to remove special caractrs after order?

    1. Yes, this can be coded of course!

  2. 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?

  3. 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? 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 *