[英]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">$30.75</span><small class="tax_label">(ex. tax)</small>
)
total = array(
'label' => Total
'value' => <span class="amount">$30.75</span>
)
相反,我想要這樣的東西:
cart_subtotal = array(
'label' => Subtotal
'value' => 30.75
)
total = array(
'label' => Total
'value' => 30.75
)
嘗試這個
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
);
}
對的,這是可能的。 而是使用$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
);
您可以使用WC_Abstract_Order類中的所有函數方法來獲得不同的總計。
由於每個順序可能不同,因此您首先需要在if
語句中使用empty()
函數內部來測試其中的某些方法……
對於總訂單,您還可以使用get_post_meta()
函數:
$order_total = get_post_meta( $order->id, '_order_total', true);
你也可以使用這些其他meta_key
,你可以在找到wp_postmeta
數據庫表中的order ID
與get_post_meta()
函數進行,例如,自定義計算:
$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中的 所有數據 (包括客戶詳細信息,訂單項目以及更多其他信息……),則應僅用於視圖測試:
echo var_dump( $order->get_order_item_totals() );
參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.