WooCommerce show custom column

2019-01-08 17:56发布

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?

5条回答
萌系小妹纸
2楼-- · 2019-01-08 18:12

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 );
}
查看更多
Deceive 欺骗
3楼-- · 2019-01-08 18:18

I came across Woocommerce now. Have added a custom field Personal Registration Number - and now wanted it to be displayed in the Order overview page.

I've manage to add the column - but still couldn't get the value of the custom field for each order.

Here is what I did:

// Removed Existing Order Page collumns
remove_filter('manage_edit-shop_order_columns', 'woocommerce_edit_order_columns');

// Added My own filter to Show the PRN - Personal Registration field
add_filter('manage_edit-shop_order_columns', 'omak_edit_order_columns');

// The omak_edit_order_columns definition
/*** Taken from admin/post_types/shop_order.php ***/
function omak_edit_order_columns($columns){
    global $woocommerce;

    $columns = array();

    $columns["cb"]              = "<input type=\"checkbox\" />";
    $columns["order_status"]        = __( 'Status', 'woocommerce' );    
    $columns["order_title"]         = __( 'Order', 'woocommerce' );
    $columns["order_prn"]           = __( 'PRN', 'woocommerce' );    // This is the line which added the column after the Title Column
    $columns["billing_address"]     = __( 'Billing', 'woocommerce' );
    $columns["shipping_address"]    = __( 'Shipping', 'woocommerce' );
    $columns["total_cost"]      = __( 'Order Total', 'woocommerce' );
    $columns["order_comments"]  = '<img alt="' . esc_attr__( 'Order Notes', 'woocommerce' ) . '" src="' . $woocommerce->plugin_url() . '/assets/images/order-notes_head.png" class="tips" data-tip="' . __( 'Order Notes', 'woocommerce' ) . '" width="12" height="12" />';

    $columns["note"]                = '<img src="' . $woocommerce->plugin_url() . '/assets/images/note_head.png" alt="' . __( 'Customer Notes', 'woocommerce' ) . '" class="tips" data-tip="' . __( 'Customer Notes', 'woocommerce' ) . '" width="12" height="12" />';

    $columns["order_date"]          = __( 'Date', 'woocommerce' );
    $columns["order_actions"]       = __( 'Actions', 'woocommerce' );

    return $columns;
}

Let me know if it helps you...

I am left to deal with how to get its values for each Order.

As commented: the function definition exists in shop_order.php in WooCommerce plugin. Let me know if anybody sorts it out.. or knows how to do it.

Thanks, (sorry, was busy in something so couldn't read back to check for errors)

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-08 18:26

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;
    }
}
查看更多
迷人小祖宗
5楼-- · 2019-01-08 18:27

The following works for WooCommerce 2.6.2. You should look into two new hooks:

1. Define columns headers

add_filter('woocommerce_admin_order_item_headers', 'so13683162_headers');
function so13683162_headers($order) {
    echo "<th>FIELD1</th>";
}

2. Populate values in rows

add_filter('woocommerce_admin_order_item_values', 'so13683162_values');
function so13683162_values($product) {
    if (isset($product -> id)) {
        $attrs = get_post_meta($product -> id, "_product_attributes", true);
        echo "<td>" . $attrs["FIELD1"]["value"] . "</td>";
    }
}
查看更多
祖国的老花朵
6楼-- · 2019-01-08 18:29

Try this, you will get your solution just write below code on your function.php file.

add_filter( 'manage_edit-shop_order_columns','your_function_name',10 );
function your_function_name($columns){
        $columns['delivery_date'] = __('Delivery date','textdomain');
        return $columns;
    }
add_action( 'manage_shop_order_posts_custom_column','your_other_function_name',20 );
function  your_other_function_name($column)
{
    swith($column)
    {
        case 'delivery_date':  // your custom code here and do what you want.
    }
}
查看更多
登录 后发表回答