How can I get all order totals without price formatting 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
)
Try This
if ( $subtotal = (float)$order->get_subtotal()) {
$total_rows[] = array(
'title' => 'Subtotal:',
'value' => $subtotal
);
}
if ($cart_discount = (float)get_post_meta($order_id, '_cart_discount', true)) {
$total_rows[] = array(
'title' => 'Discount:',
'value' => $order->cart_discount
);
}
if ($order_shipping = (float)get_post_meta($order_id, '_order_shipping', true)) {
$total_rows[] = array(
'title' => 'Shipping:',
'value' => $order_shipping
);
}
if ($order_tax = (float)get_post_meta($order_id, '_order_tax', true)) {
$total_rows[] = array(
'title' => 'tax:',
'value' => $order_tax
);
}
if ($gettotals = (float)$order->get_total()){
$total_rows[] = array(
'title' => 'Total:',
'value' => $gettotals
);
}
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();
$cart_subtotal = array(
'label' => 'Subtotal',
'value' => $order_subtotal
);
$cart_total = array(
'label' => 'Total',
'value' => $order_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 anif
statement using inside itempty()
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: