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

admin2025-06-06  1

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 / . 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 .

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/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 .

Share Improve this question edited Jan 23, 2016 at 8:38 Ron asked Jan 21, 2016 at 11:48 RonRon 3351 gold badge4 silver badges13 bronze badges 2
  • The first code block in your question, modifies the default 'Phone no' field. It does not add any new field to checkout form. The checkout fields won't be available under "custom fields", so please don't search it there. If you want to edit any of the field from billing address or shipping address, go to backend and click on any order under order list. Then you will see a small edit icon besides 'Billing Details' and 'Shipping Details' headings. Clicking on that will allow you to edit those details. I hope this helps. – Prasad Nevase Commented Jan 22, 2016 at 8:06
  • Please check my code now – Ron Commented Jan 23, 2016 at 7:51
Add a comment  | 

2 Answers 2

Reset to default 30

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.

Here is the solution : Accessing directly product data is not allowed, e.g.

$product->id

The correct method going forward is:

$product->get_id()
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749141148a316714.html

最新回复(0)