获取 Woocommerce 中格式化的订单项目行总计

Get order item line total formatted in Woocommerce

我的网站上有这段代码:

$order_items = $order->get_items();
foreach ( $order_items as $item_id => $item ) {
    $item_total = wc_get_order_item_meta( $item_id, '_line_total', true );
}

此 returns 项总计为浮点值。但是现在我怎样才能得到这个作为格式化值呢?

目前:1500
目标:1.500,00 €

有这个功能吗?还是我需要自己写代码来接收这个结果?

您正在寻找 wc_price() 函数:

Format the price with a currency symbol.

例如:

<?php wc_price($price) ?>

就这样使用WC_Abstract_Order get_formatted_line_subtotal()专用方法:

foreach ( $order->get_items() as $item_id => $item ) {
    echo $order->get_formatted_line_subtotal( $item );
}

已测试并有效。

It's already used by Woocommerce on the related templates and it handles everything needed.


您还可以使用 WC_Order_Item_Product get_subtotal() or get_total() 方法和 wc_price() 格式化价格函数,例如:

foreach ( $order->get_items() as $item_id => $item ) {
    echo wc_price( $item->get_subtotal() ); // Non discounted
    echo wc_price( $item->get_total() ); // Discounted
}