Search code examples
phpwordpresswoocommercemetadataorders

Show Woocommerce custom checkout field value in admin order making them editable


I am using "Show hide custom WooCommerce checkout field based on selected payment method" answer to one of my questions, to show / hide a custom checkout billing field, and it works fine.

Question: Is it possible to show my Custom field in WooCommerce orders in the admin panel?


Solution

  • To display "billing_options" custom checkout billing field value in admin order pages on the billing information column, use the following:

    add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_billing_options_value_in_admin_order', 10, 1 );
    function display_billing_options_value_in_admin_order($order){
        if( $value = get_post_meta( $order->get_id(), '_billing_options', true ) )
            echo '<p><strong>'.__('Invoice Number', 'woocommerce').':</strong> ' . $value . '</p>';
    }
    

    enter image description here

    To make this custom checkout billing field appear as editable in backend use the following:

    add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1 );
    function custom_admin_billing_fields( $fields ) {
        $fields['options'] = array(
            'label' => __('Invoice Number', 'woocommerce'),
            'show'  => true,
        );
        return $fields;
    }
    

    enter image description here

    Code goes in function.php file of your active child theme (or active theme). tested and works.