9

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 .

  • 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 Jan 22 '16 at 8:06
  • Please check my code now – Ron Jan 23 '16 at 7:51
19

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

  • ""Any custom field key name which starts with _ (underscore) is a hidden custom field & won't show up on backend under "Custom Fields". "" from where you get this information ? – Ron Jan 24 '16 at 11:33
  • 2
    On WordPress codex. You may get more info here – Prasad Nevase Jan 25 '16 at 14:00
  • Please helpt solve my friend question stackoverflow.com/questions/49505056/… – Ron Mar 27 '18 at 6:57
  • in my case,I got results by using get_post_meta( $order->get_id(), '_billing_phone_new', true ). I had to put underscore in front of variable name in get_post_meta function. – Dashrath May 19 at 16:58
1

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

$product->id

The correct method going forward is:

$product->get_id()

protected by Community Nov 29 '18 at 15:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.