How to Alter WooCommerce Order Field after Checkout?

In WooCommerce, you might need to alter or correct a few things after order checkout. It includes correcting punctuation, adding the right prefix to contact numbers, delete spaces, format accents, etc. Doing so, your courier partner gets the right information to deliver the parcel. So, we will learn how to alter WooCommerce order field after checkout. 

Image showing how to alter order field value after checkout or order placed

PHP Snippet: Alter WooCommerce Order Field Value after order is placed

Further, you will learn how to change contact information after checkout in WooCommerce. You can add prefix before contact number if it is not there. Also, we will learn about the WooCommerce  get_country_calling_code() function.

add_action( 'woocommerce_thankyou', 'phpsof_alter_checkout_fields_after_order' );
 
function phpsof_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 do you add this snippet?

This is how you can change WooCommerce order field value like contact number after order is placed. You can enter the right prefix if it is not entered before by customer. Also, one can edit or modify other fields to provide the right information to the courier partner. It is easier to activate this function with this code.

So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. You can place it before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. Make sure that you are editing these files in a right manner for best results. So, this way, you can learn about alter WooCommerce order field after checkout.

Is this snippet still valid?

I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected. Share it further if you find this snippet useful for you and it had saved your time.

Also Read, How to Enable WooCommerce Redirect My Account Tab to URL function?

How to Disable Order Email for Free Orders in WooCommerce?

Important links: WooCommerce – https://woocommerce.com/