将“小计”添加到“我的帐户/订单”表

时间:2019-10-02 11:48:33

标签: woocommerce woocommerce-theming

尝试在woocommerce my-account / orders表中添加订单小计,但似乎无法显示它。当前,它添加列和标签,但不显示订单总计。我目前正在使用以下代码:

add_filter( 'woocommerce_account_orders_columns', 
'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
$columns['item_subtotal_tax_excl'] = __( 'Sub-total', '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 ( $value = $order->get_meta( 'item_subtotal_tax_excl' ) ) {
    echo esc_html( $value );
}
}

2 个答案:

答案 0 :(得分:1)

WooCommerce订单元数据中不存在购物车中的小计,因此您需要获取它并从订单商品中进行计算:

add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
    $columns['item_subtotal_tax_excl'] = __( 'Sub-total', 'woocommerce' );

    return $columns;
}

add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'display_account_orders_column_rows_value' );
function display_account_orders_column_rows_value( $order ) {
    $subtotal = 0; // Initializing

    // Loop through order items (line items)
    foreach ( $order-get_items() as $item ) {
        // Sum item subtotal excluding taxes and not discounted 
        $subtotal += $item->get_subtotal(); 
    }

    echo $subtotal;
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

相关:

答案 1 :(得分:0)

编辑:以下代码对我有用:(由helgatheviking提供的关于woocommerce松弛的答案)

//show sub total in order page

add_filter( 'woocommerce_account_orders_columns', 
'add_account_orders_column', 10, 1 );

function add_account_orders_column( $columns ){

$columns['item_subtotal_tax_excl'] = __( 'Sub-Total', 
'woocommerce' );

return $columns;

}

add_action( 
'woocommerce_my_account_my_orders_column_item_subtotal_tax_excl', 
'add_account_orders_column_rows' );

function add_account_orders_column_rows( $order ) {

// Example with a custom field

if ( $value = $order->get_subtotal() ) {

echo esc_html( $value );

}
}