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

  • 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

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 *