wordpresswoocommerceadd-filter

WooCommerce not letting me add custom column to shop_order post type


I just started writing a plugin and ran into a problem right away. I want to add a column to the order overview page in the WooCommerce admin. The straight forward filter below doesn't do anything. Replacing shop_order with post or product, however, does show the extra column on the respective overview page.

add_filter('manage_edit-shop_order_columns', 'add_sales_column'); 
function add_sales_column($columns) {
    $columns['order_sales'] = "Sales";
    return $columns;
}

Trying this on:
WC Version: 2.1.5
WP Version: 3.8.1

How to solve this?


Solution

  • The problem is that we have to wait for WooCommerce to finish its setup. This goes two ways, first running all our hooks inside a plugins_loaded main hook, and then setting the priority of our hooks for something greater than WC's.

    add_action( 'plugins_loaded', 'setup_so_22237380' );
    
    function setup_so_22237380() 
    {
        // Just to make clear how the filters work
        $posttype = "shop_order";
    
        // Priority 20, with 1 parameter (the 1 here is optional)
        add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 ); 
    
        // Priority 20, with 2 parameters
        add_action( "manage_{$posttype}_posts_custom_column", 'column_display_so_22237380', 20, 2 ); 
    
        // Default priority, default parameters (zero or one)
        add_filter( "manage_edit-{$posttype}_sortable_columns", 'column_sort_so_22237380' ); 
    }
    
    
    function column_set_so_22237380( $columns )
    {
        $columns['order_sales'] = "Sales";
        return $columns;
    }
    
    
    function column_display_so_22237380( $column_name, $post_id ) 
    {
        if ( 'order_sales' != $column_name )
            return;
    
        $sales_information = 'Your custom get_order_sales_information($post_id)';
    
        if ( $sales_information )
            echo "<strong style='color:#f00;'> $sales_information </strong>";
    }
    
    function column_sort_so_22237380( $columns ) 
    {
        $columns['order_sales'] = 'order_sales';
        return $columns;
    }
    

    Setting the column as sortable is optional (manage_edit-{$posttype}_sortable_columns) and needs an extra action hook to make it work (pre_get_posts). This may be a complex function to build and requires its own research.