Search code examples
phpwordpresswoocommercehook-woocommerceorders

Custom columns in admin orders list disapeared with WooCommerce 8.6.1


Up until WooCommerce 8.4 the following code worked very nicely to add "Shipping method" and "Payment method" columns to orders view, and it showed the details exactly as needed. Default shipping and payment columns do not show all the details we need.

Now, after WooCommerce 8.6.1 update, these 2 columns do not appear, with or without High-Performance Order Storage (HPOS) enabled. The code was in the functions.php file, these two additional columns also appeared here in the display settings:

enter image description here

I understand that due to changes in WooCommerce release, I should adjust the code accordingly, but I'm not sure how exactly. I initially thought HPOS is the difference, but even when I switched back to legacy mode on 8.6.1, the two columns did not re-occur.

// Add shipping method and payment method columns to order page
add_filter( 'manage_edit-shop_order_columns', 'add_custom_columns_to_admin_orders', 20);
function add_custom_columns_to_admin_orders( $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_shipping'] = __( 'Tarnemeetod', 'my-textdomain' );
            $new_columns['order_payment'] = __( 'Maksemeetod', 'my-textdomain' );
        }
    }
    return $new_columns;
}


add_action( 'manage_shop_order_posts_custom_column', 'custom_columns_content_in_admin_orders' );
function custom_columns_content_in_admin_orders( $column ) {
    global $post, $the_order;

    if ( 'order_shipping' === $column )
    {
        echo $the_order->get_shipping_method();
    }
    if ( 'order_payment' === $column ) {
        echo $the_order->get_payment_method_title();
    }
}
// Make payment methods searchable
add_filter( 'woocommerce_shop_order_search_fields', 'makse_otsing' );
function makse_otsing( $meta_keys ){
    $meta_keys[] = '_payment_method_title';
    return $meta_keys;
}
// Make shipping methods searchable
add_filter( 'woocommerce_shop_order_search_fields', 'tarne_otsing' );
function tarne_otsing( $meta_keys ){
    $meta_keys[] = '_shipping_method';
    return $meta_keys;
}

Solution

  • I have tested your code on last WooCommerce version on Classic Order (not HPOS), and your code work in the latest WooCommerce version, so there is something else that is making trouble.

    Based on Filtering orders list in WooCommerce with HPOS answer thread, Here is the way to make your code compatible with High-Performance Order Storage (HPOS):

    // Add shipping method and payment method columns to order page
    add_filter( 'manage_woocommerce_page_wc-orders_columns', 'add_custom_columns_to_admin_orders', 20); // HPOS
    add_filter( 'manage_edit-shop_order_columns', 'add_custom_columns_to_admin_orders', 20);
    function add_custom_columns_to_admin_orders( $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_shipping'] = __( 'Tarnemeetod', 'my-textdomain' );
                $new_columns['order_payment'] = __( 'Maksemeetod', 'my-textdomain' );
            }
        }
        return $new_columns;
    }
    
    add_action('manage_woocommerce_page_wc-orders_custom_column', 'custom_columns_content_in_admin_orders', 10, 2); // HPOS
    add_action( 'manage_shop_order_posts_custom_column', 'custom_columns_content_in_admin_orders', 10, 2);
    function custom_columns_content_in_admin_orders( $column, $order ) {
        if( ! is_a($order, 'WC_order') && $order > 0 ) {
            $order = wc_get_order( $order );
        }
    
        if ( 'order_shipping' === $column ) {
            echo $order->get_shipping_method();
        } 
        elseif ( 'order_payment' === $column ) {
            echo $order->get_payment_method_title();
        }
    }
    
    // Make payment methods searchable
    add_filter( 'woocommerce_order_table_search_query_meta_keys', 'makse_ja_tarne_otsing' ); // HPOS
    add_filter( 'woocommerce_shop_order_search_fields', 'makse_ja_tarne_otsing' );
    function makse_ja_tarne_otsing( $meta_keys ){
        $meta_keys[] = '_payment_method_title';
        $meta_keys[] = '_shipping_method';
    
        return $meta_keys;
    }
    

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

    Tested and work on WooCommerce last version (8.6.1) with or without High-Performance Order Storage (HPOS) enabled.

    enter image description here

    Be sure that on "Screen options" tab (located in the upper right), your 2 custom columns are still displayed/enabled.

    Check that your code is on the right functions.php file of your active theme (or child theme).

    If your custom columns are still not displayed, first make a database backup. Then try to disable all plugins except WooCommerce. Then check if the columns are displayed.

    • If it's the case, a plugin is making trouble: Enable the plugins one by one, checking each time in between.
    • If it's not the case, re-enable your plugins, and try to remove temporarily your other custom code (keeping this custom code displaying 2 columns in admin orders list... And so on…