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

currently i add a custom billing field in woocommerce by

function custom_override_checkout_fields( $fields ) {
     $fields['billing']['billing_phone_new'] = array(
        'label'     => __('Phone 2', 'woocommerce'),
    'placeholder'  => _x('Phone 2', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}

add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');

i need to edit this field value in admin side . Currently i can edit all other values in billing address but this value is not appearing in admin section . I use the following code only for to see the value in admin section .

function order_phone_backend($order){
    echo "<p><strong>Billing phone 2:</strong> " . get_post_meta( $order->id, '_billing_phone_new', true ) . "</p><br>";
} 

add_action( 'woocommerce_admin_order_data_after_billing_address', 'order_phone_backend', 10, 1 );

I read the documentation https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ .
But everything in this document working correct expect billing_phone/Phone is note see under Custom field . I check the screen option but i already ticked custom field . Other custom field and its value are visible and editable .

How can i edit this value in back end . Please help .

2

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