Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 448
Christoph S. Ackermann Avatar asked Dec 03 '12 12:12

Christoph S. Ackermann


People also ask

How do I show custom fields in WordPress?

By default, the custom fields option is hidden in WordPress. To enable this feature, access your page or post editor. Locate the Screen Options button on the right corner of your screen, and check the Custom Fields box. This will make a new dialog box appear below the content editor.

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.


1 Answers

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 );
}
like image 72
post_ahead Avatar answered Sep 21 '22 16:09

post_ahead