Update order meta after an order is placed in WooCommerce

Update order meta after an order is placed in WooCommerce on your child site.

Snippet Type

Execute on Child Sites

Snippet

add_action( 'woocommerce_thankyou', 'wc_alter_checkout_fields_after_order' );
 
function wc_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 );   
   }   
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.