Mr. Jo
Mr. Jo

Reputation: 5261

Get order item line total formatted in Woocommerce

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?

Upvotes: 0

Views: 4017

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253901

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
}

Upvotes: 3

BenM
BenM

Reputation: 53198

You're looking for the wc_price() function:

Format the price with a currency symbol.

For example:

<?php wc_price($price) ?>

Upvotes: 1

Related Questions