2
votes

During the checkout process, under Company, I have another field for Company Registration Number. Now, everything works fine, the field is correctly saved under each order but I was wondering how can I display the company registration number under company name when I open the order in the admin panel.

PS: field name is billing_cif

Order meta in WooCommerce Admin Panel

1
Normally We expected you to provide in your question your own real code attempt. As there is some existing code for it as an official WooCommerce code snippet (but with an error), I have answered bellow exceptionally. Other more elaborated ways (but more complicated) requires from you a real code attempt in your question. - LoicTheAztec

1 Answers

3
votes

From this WooCommerce snippet found in Customizing checkout fields using actions and filters (where there is a little mistake), try the following:

/**
 * 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>'.__('My Field').':</strong><br>' . get_post_meta( $order->get_id(), 'billing_cif', true ) . '</p>';
}

You need to be sure that the correct meta_key under wp_postmeta table is billing_cif as it could be saved also like _billing_cif instead.

Code goes in functions.php file of the active child theme (or active theme). It should works.


Or the same thing in a better way since WooCommerce 3:

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 ){
    $billing_cif = $order->get_meta('billing_cif'); // Get custom field value

    if( ! empty( $billing_cif ) ) {
        echo '<p><strong>'.__('CIF reference').':</strong><br>' . $billing_cif . '</p>';
    }
}

Code goes in functions.php file of the active child theme (or active theme). It should works too.