WooCommerce展示自定义专栏

时间:2012-12-03 12:22:02

标签: wordpress wordpress-plugin woocommerce

我想在WooCommerce的后端显示一个额外的列(在Orders概述中)。 该列应包含我定义的自定义字段(交付日期)。

怎么做?

5 个答案:

答案 0 :(得分:36)

如果有人仍然需要它 - 有关如何在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 );
}

答案 1 :(得分: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中。 如果有人对它进行分类,或者知道该怎么做,请告诉我。

谢谢, (抱歉,忙于某些东西所以无法回读以检查错误)

答案 2 :(得分: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>";
    }
}

答案 3 :(得分:0)

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

答案 4 :(得分:0)

试试这个,你会得到你的解决方案,只需在 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.
    }
}