在 Woocommerce 的管理订单列表中为支付网关添加一列

Add a column for a payment gateway on admin Orders list in Woocommerce

我有一个自定义支付网关插件,我需要在 woocommerce 订单列表中包含一个自定义列,以显示来自支付网关的交易状态。是否有任何钩子可用于在支付网关插件中编写此代码?

class WC_xxxxx_Gateway extends WC_Payment_Gateway {

  public function __construct() {
 add_filter( 'manage_edit-shop_order_columns', 'wc_new_order_column' );
    }

     public function wc_new_order_column($columns){
        $columns['my_column'] = 'transaction status';
        return $columns;
       } // no output

     }

不能在管理员订单列表中只为一种付款方式(网关)添加一列并且您不需要扩展 WC_Payment_Gateway Class 以将自定义列添加到管理员订单列表。

首先只需添加所有支付网关的列,您可以根据您的自定义支付方式为每个订单自定义显示的值。

为此,您需要找出自定义支付网关的支付方式 ID(用正确的支付方式 ID 替换代码 paypal

然后您可以在下面的第二个函数中添加条件,以根据需要显示与您的自定义支付网关相关的内容"status"。

add_filter( 'manage_edit-shop_order_columns', 'payment_gateway_orders_column' );
function payment_gateway_orders_column( $columns ) {
    $new_columns = array();

    foreach ( $columns as $column_key => $column_label ) {
        if ( 'order_total' === $column_key ) {
            $new_columns['transaction_status'] = __('Transaction status', 'woocommerce');
        }

        $new_columns[$column_key] = $column_label;
    }
    return $new_columns;
}

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

    // HERE below set your targeted payment method ID
    $payment_method_id = 'paypal';

    if( $column  == 'transaction_status' ) {
        if( $the_order->get_payment_method() === $payment_method_id ) {
            // HERE below you will add your code and conditions
            $output = 'some status';
        } else {
            $output = '-';
        }

        // Display
        echo '<div style="text-align:center;">' . $output . '</div>';
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件,或自定义插件文件。已测试并有效。