获得订单总数的订单id没有格式在WooCommerce


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

参考:

  • WooCommerce Class WC_Abstract_Order methods
  • WordPress代码参考- get_post_meta