Search code examples
phpwordpresswoocommerceordersitems

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?


Solution

  • 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
    }