Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

php - How to display custom checkout billing field in WooCommerce admin single orders

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...