在 Woocommerce 管理订单列表中显示自定义元字段并使其可搜索 [英] Display a custom meta field in Woocommerce admin orders list and make it searchable

查看:131
本文介绍了在 Woocommerce 管理订单列表中显示自定义元字段并使其可搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用下面的 function.php 代码在结账时提供一个多选字段.我们将其添加到订单页面并将其包含在电子邮件中.

We use the following function.php code below to offer a multi choice field in checkout. We add it to the order page and include it in the email.

我们如何将它也包含在订单概览管理页面中,使其可搜索和排序?

How can we include it in the ORDERs overview admin page as well, make it searchable and sortable ?

// add select box for platform used at checkout
add_action('woocommerce_after_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
    echo '<h3>'.__('').'</h3>';
    woocommerce_form_field( 'platform', array(
        'type'          => 'select',
        'class'         => array( 'wps-drop' ),
        'label'         => __( 'On which platform will you be using it?' ),
        'options'       => array(
            'blank'     => __( '-- Choose an option --', 'wps' ),
            'app1'  => __( 'app1', 'wps' ),
            'app2'  => __( 'app2', 'wps' ),
            'app3'  => __( 'app3', 'wps' )
        )
    ), $checkout->get_value( 'platform' ) );
}

// Process the checkout
 add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
 function wps_select_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if ($_POST['platform'] == "blank")
        wc_add_notice( '<strong>Please select your primary trading platform that you will be using with our strategy</strong>', 'error' );
 }


 // Update the order meta with field value
 add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
 function wps_select_checkout_field_update_order_meta( $order_id ) {
     if ($_POST['platform']) update_post_meta( $order_id, 'platform', esc_attr($_POST['platform']));
 }

 // Display field value on the order edition page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
function wps_select_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Primary Platform').':</strong> ' . get_post_meta( $order->id, 'platform', true ) . '</p>';
}

// Add selection field value to emails
add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
function wps_select_order_meta_keys( $keys ) {
    $keys['platform:'] = 'platform';
    return $keys;
}

推荐答案

以下是在 Admin WooCommerce 订单列表中添加自定义列平台"并在搜索中添加此自定义字段元键的方法:

Here is the way to add a custom column 'Platform' in Admin WooCommerce Orders list and to add this custom field meta key in the search:

// ADDING A CUSTOM COLUMN TITLE TO ADMIN ORDER LIST
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 12, 1 );
function custom_shop_order_column($columns)
{
    // Set "Actions" column after the new colum
    $action_column = $columns['order_actions']; // Set the title in a variable
    unset($columns['order_actions']); // remove  "Actions" column


    //add the new column "Platform"
    $columns['order_platform'] = '<span>'.__( 'Platform','woocommerce').'</span>'; // title

    // Set back "Actions" column
    $columns['order_actions'] = $action_column;

    return $columns;
}

// ADDING THE DATA FOR EACH ORDERS BY "Platform" COLUMN
add_action( 'manage_shop_order_posts_custom_column' , 'custom_order_list_column_content', 10, 2 );
function custom_order_list_column_content( $column, $post_id )
{

    // HERE get the data from your custom field (set the correct meta key below)
    $platform = get_post_meta( $post_id, 'platform', true );
    if( empty($platform)) $platform = '';

    switch ( $column )
    {
        case 'order_platform' :
            echo '<span>'.$platform.'</span>'; // display the data
            break;
    }
}

// MAKE 'PLATFORM' METAKEY SEARCHABLE IN THE SHOP ORDERS LIST
add_filter( 'woocommerce_shop_order_search_fields', 'platform_search_fields', 10, 1 );
function platform_search_fields( $meta_keys ){
    $meta_keys[] = 'platform';
    return $meta_keys;
}

代码位于活动子主题(活动主题或任何插件文件)的 function.php 文件中.

经过测试并有效.

这篇关于在 Woocommerce 管理订单列表中显示自定义元字段并使其可搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆