How to display custom field in woocommerce orders in admin panel?

The code you have provided is incomplete. Not sure if that is the only code you are using to achieve what you want. So, besides first code block which you have provided, bellow I am adding all rest of the code which is required to show the new field on backend in ‘Order Details’ box and make it editable through custom fields. Please note, in your second code block you have named the field key as _billing_new_phone. Any custom field key name which starts with _ (underscore) is a hidden custom field & won’t show up on backend under “Custom Fields”.

/**
 * Process the checkout
 */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['billing_phone_new'] )
        wc_add_notice( __( 'Phone 2 is compulsory. Please enter a value' ), 'error' );
}


/**
 * Update the order meta with field value
 */
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['billing_phone_new'] ) ) {
        update_post_meta( $order_id, 'billing_phone_new', sanitize_text_field( $_POST['billing_phone_new'] ) );
    }
}


/**
 * Display field value on the order edit page
 */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Phone 2').':</strong> <br/>' . get_post_meta( $order->get_id(), 'billing_phone_new', true ) . '</p>';
}

WooCommerce does not make the new checkout field editable under its standard ‘Order Details’ box. It will be available as ‘view only’ mode in that box but you can edit the same through WordPress’ standard custom fields block. See below screenshot.

enter image description here

Leave a Comment