skip to Main Content

I need help to display custom order metadata in WooCommerce orders list. I have added a new custom column called "Meeting Points" and "Meeting Date&Time."

enter image description here

Here is my code so far:

add_filter('manage_edit-shop_order_columns', 'add_meeting_points_column');
function add_meeting_points_column($columns) {
    $columns = array_merge(
        array_slice($columns, 0, array_search('order_date', array_keys($columns)), true),
        array('meeting_points' => __('Meeting Points', 'text-domain')),
        array_slice($columns, array_search('order_date', array_keys($columns)), null, true)
    );
    return $columns;
}

add_action('manage_shop_order_posts_custom_column', 'display_meeting_points_column', 20, 2);
function display_Meeting_Points_column($column, $post_id) {
    if ('Meeting_Points' === $column) {
        $order = wc_get_order($post_id);

        if ($order) {
            $meeting_points = wc_get_order_meta_keys($order->get_keys(), 'MeetingPoints', true);
            echo !empty($meeting_points) ? esc_html($meeting_points) : esc_html__('Not Available', 'text-domain');
        }
    }
}

My code doesn’t work as expected, it doesn’t display the data.

enter image description here

What I am doing wrong? Your assistance will be appreciated.

2

Answers


  1. There are multiple mistakes in your 2nd function, for example column string value should be'meeting_points' instead of 'Meeting_Points', to populate the column with your custom metadata.

    Try the following code replacement (making sure that MeetingPoints and MeetingDate&Time are the correct meta keys used in wp_postmeta database table for your orders):

    add_filter('manage_edit-shop_order_columns', 'add_meeting_points_column');
    function add_meeting_points_column($columns) {
        $columns = array_merge(
            array_slice($columns, 0, array_search('order_date', array_keys($columns)), true),
            array(
                'meeting_points'   => __('Meeting Points', 'text-domain'),
                'meeting_datetime' => __('Meeting DateTime', 'text-domain'),
            ),
            array_slice($columns, array_search('order_date', array_keys($columns)), null, true)
        );
        return $columns;
    }
    
    add_action('manage_shop_order_posts_custom_column', 'display_meeting_points_column', 20, 2);
    function display_Meeting_Points_column($column, $post_id) {
        global $the_order;
    
        if ($column === 'meeting_points') {
            echo ($value = $the_order->get_meta('MeetingPoints')) ? esc_html($value) : esc_html__('Not Available', 'text-domain');
        } elseif ($column === 'meeting_datetime') {
            echo ($value = $the_order->get_meta('MeetingDate&Time')) ? esc_html($value) : esc_html__('Not Available', 'text-domain');
        }
    }
    

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

    In admin orders list:

    enter image description here

    In database under wp_postmeta table:

    enter image description here

    Related:

    Login or Signup to reply.
  2. It will help to get the right meta key values. You can add this code to functions.php and then visit an order page in the Dashboard to see a list of all the meta keys for each product in the order in the output in the footer.

    add_filter( 'woocommerce_display_admin_footer_text', 'chad_woocommerce_show_order_meta_on_order_page');
    function chad_woocommerce_show_order_meta_on_order_page() { 
        $order = new WC_Order( get_the_ID() );
        if( $order ){
            foreach( $order->get_items() as $item_id => $item ){
                $product_id = $item['product_id'];
                $custom_fields = get_post_custom($product_id);
                foreach( $custom_fields as $key => $value){
                    echo '<br>'.$key;
                }
                echo '<br><br>';
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search