Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add columns to admin orders list in WooCommerce

I am using WooCommerce plugin for one of my ecommerce WordPress websites. I want to add some columns to my order listing page in the WooCommerce admin area. I am not able to find out where to add that.

Can anyone advise which template page I need to amend in order to meet my requirement?

like image 271
Upendra Sharma Avatar asked Apr 06 '16 09:04

Upendra Sharma


People also ask

How do I add a column in WooCommerce orders page?

WooCommerce Orders List Column, Step 1: Add a Header. Adding a new column is straight-forward using the manage_edit-{$post_type}_columns filter from WordPress core. This will let us change the columns shown in the list table for orders so we can inject our own.

How do I add a column to my WordPress admin?

For instance, you can click the 'Edit' button for any column and then change its type, label, and width. In addition to the default columns, you can add more admin columns by clicking the '+ Add Column' button. For example, we added the featured image admin column for the posts screen.

How do I display custom fields in WooCommerce orders in admin panel?

First, to create a field, go to WooCommerce > Custom Order Fields. Click “Add Field” and begin creating your order field. The “label” is the field name, and will be displayed in the order details. The “description” will be displayed to the user upon hovering over the “?” symbol.


1 Answers

Updated: 2018-03-30 - added positioning feature to the new columns

So you if you want to add some columns in the orders Admin list page (in backend):

ADDING COLUMNS IN WOOCOMMERCE ADMIN ORDERS LIST

In the example below, we add 2 new custom columns, before existing "Total" and "Actions" columns.

// ADDING 2 NEW COLUMNS WITH THEIR TITLES (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns)
{
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['my-column1'] = __( 'Title1','theme_domain');
            $reordered_columns['my-column2'] = __( 'Title2','theme_domain');
        }
    }
    return $reordered_columns;
}

// Adding custom fields meta data for each new column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    switch ( $column )
    {
        case 'my-column1' :
            // Get custom post meta data
            $my_var_one = get_post_meta( $post_id, '_the_meta_key1', true );
            if(!empty($my_var_one))
                echo $my_var_one;

            // Testing (to be removed) - Empty value case
            else
                echo '<small>(<em>no value</em>)</small>';

            break;

        case 'my-column2' :
            // Get custom post meta data
            $my_var_two = get_post_meta( $post_id, '_the_meta_key2', true );
            if(!empty($my_var_two))
                echo $my_var_two;

            // Testing (to be removed) - Empty value case
            else
                echo '<small>(<em>no value</em>)</small>';

            break;
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here


Related answer (for products): Add custom columns to admin producs list in WooCommerce backend

like image 126
LoicTheAztec Avatar answered Oct 10 '22 17:10

LoicTheAztec