Bhavik Hirani Bhavik Hirani - 3 months ago 41
PHP Question

Get order totals by order id without Formating in WooCommerce

How can I get all order totals without price formating in woocommerce?

I Have tried this:


$order->get_order_item_totals()


But I get formatted prices like this:

cart_subtotal = array(
'label' => Subtotal
'value' => <span class="amount">$30.75</span><small class="tax_label">(ex. tax)</small>
)
total = array(
'label' => Total
'value' => <span class="amount">$30.75</span>
)


Instead, I would like to have something like this:

cart_subtotal = array(
'label' => Subtotal
'value' => 30.75
)
total = array(
'label' => Total
'value' => 30.75
)


Please anyone could help me with this?

Answer

Yes it is possible. Instead using $order->get_order_item_totals();, you should use:

// For Order Sub-Total:
$order_subtotal = $order->get_subtotal();

// For Order Total:
$order_total = $order->get_total();

You can use all the function methods in Class WC_Abstract_Order to get different totals.

As each order could be different, You will need to test some of this methods first with in an if statement using inside it empty() function…


For order total, you could also use get_post_meta() function:

$order_total = get_post_meta( $order->id, '_order_total', true);

You could also use these others meta_key that you can find in wp_postmeta database table for an order ID with get_post_meta() function to make, for example, custom calculations:

$order_shipping =     get_post_meta( $order->id, '_order_shipping', true);
$order_discount =     get_post_meta( $order->id, '_cart_discount', true);
$order_discount_tax = get_post_meta( $order->id, '_cart_discount_tax', true);
$order_tax =          get_post_meta( $order->id, '_order_tax', true);
$order_shipping_tax = get_post_meta( $order->id, '_order_shipping_tax', true);

// this one you get it yet
$order_total =        get_post_meta( $order->id, '_order_total', true);

If you want to have a look to all the data in one order ID (including customer details, order items, and many more thinks…), you should use just for a view test:

echo var_dump( $order->get_order_item_totals() );

Reference: