【问题标题】:Get order item line total formatted in Woocommerce获取在 Woocommerce 中格式化的订单项目总行
【发布时间】:2018-12-18 13:36:31
【问题描述】:

我的网站上有这段代码:

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

这会将项目总数作为浮点值返回。但是我现在如何才能将其作为格式化值?

目前:1500
目标:1.500,00 欧元

是否有这个功能或者我需要编写自己的代码来接收这个结果?

【问题讨论】:

  • 当然,wc_price()
  • @executable 别傻了。我问过 WooCommerce 中是否有此功能。我知道如何使用 PHP。
  • @BenM 谢谢!我会试试的:)
  • 您没有在问题中询问 WooCommerce...

标签: php wordpress woocommerce orders items


【解决方案1】:

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

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

经过测试并且有效。

Woocommerce 已经在相关模板上使用它,它可以处理所需的一切。


您还可以使用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
}

【讨论】:

  • 在我的情况下退回了 0,00 欧元。奇怪。
  • @Mr.Jo 这是不可能的,因为这是使用 on all woocommerce templates involving Orders items ......所以这意味着如果你按顺序查看你也会得到 0?根本不可能
  • 我的错。有效!
【解决方案2】:

您正在寻找wc_price() 函数:

用货币符号格式化价格。

例如:

<?php wc_price($price) ?>

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2018-10-26
    • 2017-07-20
    • 1970-01-01
    相关资源
    最近更新 更多