WooCommerce: Edit Custom Field @ Order Admin

Adding a custom field to your WooCommerce checkout is a breeze. It’s a fantastic way to collect extra information from your customers. It could be a custom Billing field, a Shipping field, or a completely unrelated custom field.

But while you can easily view the custom field data in the backend, editing it directly from the order admin is a frustratingly locked feature. This presents a significant challenge for businesses needing to modify order details post-purchase.

Luckily, a bit of custom code can fix this, giving you the flexibility and control you need to manage your orders effectively.

Let’s dive in and explore how to overcome this hurdle. Enjoy!

The snippet below will allow you to edit customer/order fields directly from the backend. For example, you may want to correct a tax number, or a custom shipping phone number.

PHP Snippet: Edit Order Custom Field Value From WooCommerce “Edit Order” Admin Page

In order to make the snippet below work, you’ll need to know the custom field “key“.

If you’ve added the custom field by custom code in the checkout page, this is easily available to you – otherwise if you’re using a plugin you will need to find out what that is.

In my case , that is “_billing_tax“, and you will notice you have to search/replace it with your custom key for a total of 6 times in the code below.

/**
 * @snippet       Edit Custom Field @ Woo Edit Order Page
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_order_custom_field_input' );

function bbloomer_order_custom_field_input( $order ) {
    echo '<div class="address">'; // REQUIRED!
    echo '<p><b>Tax number:</b> ' . $order->get_meta( '_billing_tax' ) . '</p>';
    echo '</div>';
    echo '<div class="edit_address">'; // REQUIRED!
    woocommerce_wp_text_input(
		array(
			'id'    => '_billing_tax',
			'label' => 'Tax number',
			'value' => $order->get_meta( '_billing_tax' ),
		),
		$order
	);
    echo '</div>';
}

add_action( 'woocommerce_process_shop_order_meta', 'bbloomer_order_custom_field_save' );

function bbloomer_order_custom_field_save( $order_id ) {
    if ( isset( $_POST['_billing_tax'] ) ) {
		$order = wc_get_order( $order_id );
		$order->update_meta_data( '_billing_tax', sanitize_text_field( $_POST['_billing_tax'] ) );
		$order->save();
	}
}

Once this is done, you can open the single order admin page, scroll to “Billing” and click on the pencil icon. The “Tax number” field as per this screenshot will become an editable input:

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

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 *