WooCommerce show custom column

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).

How to do this?


Solution 1:

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 );
}

Solution 2:

To add new column coupon in woo-commerce order table and get all coupon code according to order. you need to copy and paste in you function.php.

add_filter('manage_edit-shop_order_columns', 'custom_shop_order_column', 11);

function custom_shop_order_column($columns) {
    //add columns
    $columns['my-column1'] = __('Coupons Code', 'theme_slug');
    return $columns;
}

// adding the data for each orders by column (example)
add_action('manage_shop_order_posts_custom_column', 'cbsp_credit_details', 10, 2);

function cbsp_credit_details($column) {
    global $post, $woocommerce, $the_order;
    $order_id = $the_order->id;

    switch ($column) {
        case 'my-column1' :
            // $myVarOne = wc_get_order_item_meta( $order_id, 15, true );
            if ($the_order->get_used_coupons()) {

                $coupons_count = count($the_order->get_used_coupons());
                foreach ($the_order->get_used_coupons() as $coupon) {
                    echo $coupon;

                    $i++;
                }

                echo '</p>';
            }
           // echo $myVarOne;
            break;
    }
}