WooCommerce show custom column

前端 未结 5 633
梦谈多话
梦谈多话 2020-12-01 06:45

I want to show a additional column in the backend of WooCommerce (in Orders overview). The column should contain a custom field, which i have defined (delivery date).

<
5条回答
  •  孤城傲影
    2020-12-01 07:35

    In case someone still needs it - instructions on how to add new columns in Woocommerce orders list. No need in unsetting the default columns, just add this in your functions.php and your code will be valid for updates.

    1. Define columns position and names

    add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
    function MY_COLUMNS_FUNCTION($columns){
        $new_columns = (is_array($columns)) ? $columns : array();
        unset( $new_columns['order_actions'] );
    
        //edit this for you column(s)
        //all of your columns will be added before the actions column
        $new_columns['MY_COLUMN_ID_1'] = 'MY_COLUMN_1_TITLE';
        $new_columns['MY_COLUMN_ID_2'] = 'MY_COLUMN_2_TITLE';
        //stop editing
    
        $new_columns['order_actions'] = $columns['order_actions'];
        return $new_columns;
    }
    

    2. For each custom column, show the values

    add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
    function MY_COLUMNS_VALUES_FUNCTION($column){
        global $post;
        $data = get_post_meta( $post->ID );
    
        //start editing, I was saving my fields for the orders as custom post meta
        //if you did the same, follow this code
        if ( $column == 'MY_COLUMN_ID_1' ) {    
            echo (isset($data['MY_COLUMN_1_POST_META_ID']) ? $data['MY_COLUMN_1_POST_META_ID'] : '');
        }
        if ( $column == 'MY_COLUMN_ID_2' ) {    
            echo (isset($data['MY_COLUMN_2_POST_META_ID']) ? $data['MY_COLUMN_2_POST_META_ID'] : '');
        }
        //stop editing
    }
    

    3. (optional) Function to make the columns sortable

    add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
    function MY_COLUMNS_SORT_FUNCTION( $columns ) {
        $custom = array(
            //start editing
    
            'MY_COLUMN_ID_1'    => 'MY_COLUMN_1_POST_META_ID',
            'MY_COLUMN_ID_2'    => 'MY_COLUMN_2_POST_META_ID'
    
            //stop editing
        );
        return wp_parse_args( $custom, $columns );
    }
    

提交回复
热议问题