How to Update an Order Field Value After a Successful WooCommerce Order

How to Update an Order Field Value After a Successful WooCommerce Order
On this page
  1. Why Correct Order Fields After Checkout
  2. Step 1: Hook Into woocommerce_thankyou
  3. Troubleshooting
  4. Frequently Asked Questions
  5. Conclusion

You can update an order field value after a successful WooCommerce order by hooking into woocommerce_thankyou, reading the order, correcting the field, and saving it back with update_post_meta() — useful for cleaning up phone numbers, addresses, or any field a customer might enter inconsistently.

Why Correct Order Fields After Checkout

  • Shipping carriers often need a phone number with the country calling code attached, which customers rarely type themselves
  • Free-text fields collect inconsistent formatting — extra spaces, missing punctuation, mixed case
  • Fixing data once at order time is easier than cleaning it up later across hundreds of orders

Step 1: Hook Into woocommerce_thankyou

This fires right after an order is placed and gives you the order ID, which is enough to load the order object and adjust any field on it.

PHP
/** Prefix the billing phone with the country's calling code if it's missing */
add_action( 'woocommerce_thankyou', 'prefix_phone_with_calling_code' );

function prefix_phone_with_calling_code( $order_id ) {

    $order = wc_get_order( $order_id );
    $phone = $order->get_billing_phone();
    $code  = WC()->countries->get_country_calling_code( $order->get_billing_country() );
    $code  = is_array( $code ) ? $code[0] : $code;

    if ( $phone && $code && ! str_starts_with( $phone, $code ) ) {
        $order->set_billing_phone( $code . $phone );
        $order->save();
    }
}
Update an order field value after a successful WooCommerce order: the page before adding the code
Before
Update an order field value after a successful WooCommerce order: the page after adding the code
After

Trimming and Cleaning a Text Field Instead

The same pattern works for stripping unwanted characters from any field, such as a company name that sometimes includes stray punctuation.

PHP
/** Strip special characters from the billing company field after checkout */
add_action( 'woocommerce_thankyou', 'clean_billing_company_field' );

function clean_billing_company_field( $order_id ) {

    $order   = wc_get_order( $order_id );
    $company = $order->get_billing_company();
    $cleaned = preg_replace( '/[^A-Za-z0-9\s&.-]/', '', $company );

    if ( $cleaned !== $company ) {
        $order->set_billing_company( $cleaned );
        $order->save();
    }
}

Troubleshooting

str_starts_with() causes a fatal error

That function only exists on PHP 8.0+; on older PHP versions swap it for strpos( $phone, $code ) === 0 instead.

The change doesn’t stick after saving

Use the order’s own setter methods (like set_billing_phone()) plus $order->save() rather than raw update_post_meta() where possible, since WooCommerce’s order data store doesn’t always store orders as plain posts (HPOS-compatible orders live in dedicated tables).

Frequently Asked Questions

Will this run for every order or only the current one?

woocommerce_thankyou fires once per order, right after that specific order is placed, so it only touches the order the customer just completed.

Can I update custom checkout fields the same way?

Yes — if the field is saved as order meta, read it with $order->get_meta( '_your_field_key' ) and update it with update_meta_data() plus save().

Is it better to fix the value at checkout instead of after the order?

Validating at checkout is usually cleaner if you can catch the issue before submission, but post-order correction is useful when the fix depends on data (like calling codes) that’s easiest to look up once the order and country are both confirmed.

Conclusion

A single woocommerce_thankyou hook is enough to update an order field value after a successful WooCommerce order, whether you’re prefixing phone numbers or cleaning up free-text input before it reaches your fulfillment process.

See also How to Set a Checkout Field Value on Order Creation. Then read How to Automatically Update the WooCommerce Cart on Quantity Change.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call