0

I've this code on my Website:

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

This returns the item total as a float value. But how can I get this now as a formatted value?

Currently: 1500
Goal: 1.500,00 €

Is there a function for this or do I need to write my own code to receive this result?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mr. Jo
  • 4,946
  • 6
  • 41
  • 100

2 Answers2

3

Just use WC_Abstract_Order get_formatted_line_subtotal() dedicated method this way:

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

Tested and works.

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


You could also use WC_Order_Item_Product get_subtotal() or get_total() methods with wc_price() formatting price function like:

foreach ( $order->get_items() as $item_id => $item ) {
    echo wc_price( $item->get_subtotal() ); // Non discounted
    echo wc_price( $item->get_total() ); // Discounted
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Returned 0,00 € in my case. Strange. – Mr. Jo Dec 18 '18 at 13:58
  • 1
    @Mr.Jo That is not possible as this is used [on all woocommerce templates involving Orders items](https://github.com/woocommerce/woocommerce/blob/release/3.5/templates/order/order-details-item.php#L46) … So that means that if your look in order view you will get also 0? simply not possible – LoicTheAztec Dec 18 '18 at 14:02
  • My fault. Works! – Mr. Jo Dec 18 '18 at 14:12
1

You're looking for the wc_price() function:

Format the price with a currency symbol.

For example:

<?php wc_price($price) ?>
BenM
  • 52,573
  • 26
  • 113
  • 168