Search code examples
phpwordpresswoocommerceadminorders

WooCommerce Admin Order list Custom Column doesn't display values


I have "How Did You Discover Us?" info that is obtained during checkout to be included in my custom Info column within the admin orders' page in WooCommerce.

I have the same detail displayed in edit order page.

However, it isn't returning How Did You Discover Us? value in the new Info column in admin orders. It remains empty.

How can I get it to display correctly?

//Checkout Section 
add_filter('woocommerce_checkout_fields', function($fields) {   
    $fields['billing']['billing_my_custom_select'] = [      
        'label' => __('How Did You Discover Us?', 'textdomain'), 
        'required' => true,         
        'type' => 'select',         
        'options' => [  
            '' => __('Select Source', 'textdomain'), 
            'X' => __('X', 'textdomain'),       
            'Y' => __('Y', 'textdomain') ],         
            'class' => ['form-row-wide'],       
        'priority' => 110   
    ];  
    return $fields; 
});
    
//Admin Note Section
add_action('woocommerce_admin_order_data_after_billing_address', function($order) {
    $my_value = get_post_meta($order->id, '_billing_my_custom_select', true);
    
    if (!empty($my_value)) {
        echo '<p><strong>' . __('How Did You Discover Us?', 'textdomain') . ':</strong> ' . $my_value . '</p>';     
    } 
});
//End How Did You Discover Us

//Edit Admin Orders Page, New Column
add_filter('manage_edit-shop_order_columns', 'add_order_new_column_header', 20);
function add_order_new_column_header($columns){

    $new_columns = array();

    foreach ($columns as $column_name => $column_info){

        $new_columns[$column_name] = $column_info;

        if ('order_total' === $column_name) {
            $new_columns['order_details'] = __('Info', 'textdomain');
        }
    }
    return $new_columns;
} 

add_action('manage_shop_order_posts_custom_column', 'add_wc_order_admin_list_column_content');   
function add_wc_order_admin_list_column_content($column){

    global $post;

    if ('order_details' === $column){

        $order = wc_get_order( $post->ID, '_billing_my_custom_select', 1);
        echo $my_value;

    } 
}
//End Edit Admin Orders Page, New Column

Solution

  • Your code is a bit outdated since WooCommerce 3 and there are some mistakes.

    Also, you should always use explicit meta keys and always name the hooked functions.

    Try the following code replacement:

    // Add custom Checkout billing field 
    add_filter('woocommerce_checkout_fields', 'add_custom_checkout_field' );
    function add_custom_checkout_field($fields) {   
        $fields['billing']['billing_discover_us'] = [      
            'label' => __('How Did You Discover Us?', 'textdomain'), 
            'required' => true,         
            'type' => 'select',         
            'options' => [  
                '' => __('Select Source', 'textdomain'), 
                'X' => __('X', 'textdomain'),       
                'Y' => __('Y', 'textdomain') ],         
                'class' => ['form-row-wide'],       
            'priority' => 110   
        ];  
        return $fields; 
    }
        
    // Display Admin order custom Checkout billing field value
    add_action('woocommerce_admin_order_data_after_billing_address', 'display_admin_order_custom_field_value' );
    function display_admin_order_custom_field_value( $order ) {
        if ( $value = $order->get_meta('_billing_discover_us') ) {
            echo '<p><strong>' . __('How Did You Discover Us?', 'textdomain') . ':</strong> ' . $value . '</p>';     
        } 
    }
    
    
    // Admin Orders list: New Column
    add_filter( 'manage_edit-shop_order_columns', 'add_order_new_column_header', 20 );
    function add_order_new_column_header( $columns ){
        $new_columns = array();
    
        foreach ($columns as $column_name => $column_info){
            $new_columns[$column_name] = $column_info;
    
            if ( 'order_total' === $column_name ) {
                $new_columns['order_details'] = __('Info', 'textdomain');
            }
        }
        return $new_columns;
    } 
    
    // Admin Orders list: New Column content
    add_action( 'manage_shop_order_posts_custom_column', 'add_wc_order_admin_list_column_content', 20 );   
    function add_wc_order_admin_list_column_content( $column ){
        if ( 'order_details' === $column ){
            global $the_order;
    
            echo $the_order->get_meta('_billing_discover_us');
        } 
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Note: As I have renamed your custom field meta key, you will need to place an order to see the changes.

    You will get something like:

    enter image description here

    Related: How to get WooCommerce order details