在 WooCommerce 中无需格式化即可按订单 ID 获取订单总计

Get order totals by order id without Formatting in WooCommerce

如何在 WooCommerce 中获取没有价格格式的所有订单总数?

我试过这个:

$order->get_order_item_totals()

但我得到这样的格式化价格:

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

相反,我想要这样的东西:

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

是的,这是可能的。而不是使用 $order->get_order_item_totals();,你应该使用:

// 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 an if statement using inside it empty() function…


对于总订单,您还可以使用get_post_meta()函数:

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

您还可以使用其他 meta_key,您可以在 wp_postmeta 数据库 table 对于带有 get_post_meta() 函数的 order ID 进行自定义计算:

$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);

如果您想在一个订单 ID 中查看所有数据(包括客户详细信息、订单项目和许多more thinks...), 你应该只用于视图测试:

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

参考:

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
);
}