向 WooCommerce "My account" 订单添加多个自定义列 table

Add multiple custom columns to WooCommerce "My account" orders table

我正在尝试向 WooCommerce > 我的帐户 > 订单页面添加几列。

到目前为止,下面的代码添加了不止一列,但没有显示列的内容,我在哪里弄错了?

add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
    
     unset($columns['order-total']);
   $columns['custom-column'] = __( 'New Column', 'woocommerce' );
    $columns['custom-column2'] = __( 'New Column 2', 'woocommerce' );

    return $columns;
}

add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
    // Example with a custom field

    if( $columns == 'custom-column' ) {

        echo 'Hello';

    }

    if( $columns == 'custom-column2' ) {

        echo 'Hello 2';

    }
} 

woocommerce_account_orders_columns 过滤器挂钩允许我们添加 1 个或多个列

// Add new column(s) to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
    $columns['custom-column'] = __( 'New Column 1', 'woocommerce' );
    $columns['custom-column2'] = __( 'New Column 2', 'woocommerce' );

    return $columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );

但是,每列添加内容是通过 woocommerce_my_account_my_orders_column_{$column_id} 操作挂钩完成的。

因此在这种特殊情况下,{$column_id} 需要用列键别名(custom-columncustom-column2)替换。 因此无需通过 if 条件

确定回调函数中的正确列

所以你得到:

// Adds data to the custom column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_custom_column( $order ) {    
    echo 'New Column 1';
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'filter_woocommerce_my_account_my_orders_column_custom_column', 10, 1 );

// Adds data to the custom column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_custom_column2( $order ) {    
    echo 'New Column 2';
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column2', 'filter_woocommerce_my_account_my_orders_column_custom_column2', 10, 1 );