13

我想在 WooCommerce 的后端显示一个附加列(在订单概览中)。该列应包含我已定义的自定义字段(交货日期)。

这个怎么做?

4

5 回答 5

38

如果有人仍然需要它 - 有关如何在 Woocommerce 订单列表中添加新列的说明。无需取消设置默认列,只需将其添加到您的 functions.php 中,您的代码将对更新有效。

1.定义列位置和名称

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. 对于每个自定义列,显示值

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.(可选)使列可排序的功能

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 );
}
于 2013-05-16T18:42:39.937 回答
1

在woo-commerce订单表中添加新列优惠券并根据订单获取所有优惠券代码。你需要复制并粘贴到你的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;
    }
}
于 2017-02-22T06:29:52.083 回答
1

试试这个,你会得到你的解决方案,只需在你的function.php文件上写下面的代码。

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.
    }
}
于 2018-01-16T04:23:35.990 回答
0

我现在遇到了 Woocommerce。添加了一个自定义字段个人注册号 - 现在希望它显示在订单概览页面中。

我设法添加了该列 - 但仍然无法获取每个订单的自定义字段的值。

这是我所做的:

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

让我知道它是否对您有帮助...

我要处理如何获取每个订单的值。

正如评论:函数定义存在于 WooCommerce 插件的 shop_order.php 中。让我知道是否有人解决它..或知道如何去做。

谢谢,(抱歉,正忙于某事,所以无法回读以检查错误)

于 2013-04-26T22:39:22.527 回答
0

以下适用于 WooCommerce 2.6.2。你应该研究两个新的钩子:

1.定义列标题

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

2. 按行填充值

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>";
    }
}
于 2016-07-22T19:26:50.083 回答