仅在WooCommerce管理员订单列表自定义列中显示特定订单状态的数据

问题描述

我在WooCoomerce管理订单列表中有一个自定义列,用于显示客户注释。

如果订单处于特定的自定义状态wc-autoquote,我只想在订单列表中显示注释。

是否可以针对其他所有订单状态将其过滤掉?

我的自定义列的代码

add_filter('manage_edit-shop_order_columns','add_customer_note_column_header');
function add_customer_note_column_header($columns) {
    $new_columns = (is_array($columns)) ? $columns : array();

    $new_columns['order_customer_note'] = 'Customer Notes';

    return $new_columns;
}

add_action('admin_print_styles','add_customer_note_column_style');
function add_customer_note_column_style() {
    $css = '.widefat .column-order_customer_note { width: 15%; }';
    wp_add_inline_style('woocommerce_admin_styles',$css);
}


add_action('manage_shop_order_posts_custom_column','add_customer_note_column_content');
function add_customer_note_column_content($column) {
    global $post,$the_order;

    if(empty($the_order) || $the_order->get_id() != $post->ID) {
    $the_order = wc_get_order($post->ID);
    }

    $customer_note = $the_order->get_customer_note();
    if($column == 'order_customer_note') {
    echo('<span class="order-customer-note">' . $customer_note . '</span>');
}

解决方法

您可以使用以下内容指定状态

// If has order status
if ( $order->has_status( 'MY STATUS' ) ) {...

所以你得到

// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
    // Add new column
    $columns['order_customer_note'] = __( 'Customer Notes','woocommerce' );

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns','filter_manage_edit_shop_order_columns',10,1 );

// Populate the Column
function action_manage_shop_order_posts_custom_column( $column,$post_id ) {
    // Compare
    if ( $column == 'order_customer_note' ) {
        // Get order
        $order = wc_get_order( $post_id );
    
        // Has order status
        if ( $order->has_status( 'autoquote' ) ) {
            // Get customer note
            $customer_note = $order->get_customer_note();
            
            // NOT empty
            if ( ! empty ( $customer_note ) ) {
                echo '<span class="order-customer-note">' . $customer_note . '</span>';
            }
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column','action_manage_shop_order_posts_custom_column',2 );