在 WooCommerce 中获取订单总计计算的订单费用和折扣金额
Get order fee and discount amounts for order totals calculations in WooCommerce
基于
我知道如何获取购物车总数。计算中包括费用和折扣。
// Get cart subtotal & shipping total & fees & discounts
$subtotal = WC()->cart->subtotal;
$shipping_total = WC()->cart->get_shipping_total();
$fees = WC()->cart->get_fee_total();
$discount_excl_tax_total = WC()->cart->get_cart_discount_total();
$discount_tax_total = WC()->cart->get_cart_discount_tax_total();
$discount_total = $discount_excl_tax_total + $discount_tax_total;
// Cart Total
$cart_total = $subtotal + $shipping_total + $fees - $discount_total;
我还需要获取订单总数。我在获取订单折扣和费用时遇到问题。
function filter_woocommerce_get_order_item_totals( $order ) {
// Get order subtotal & shipping total & fees & discounts
$order_subtotal = $order->get_subtotal();
$order_shipping_total = $order->get_shipping_total();
$order_fees = ...
$order_discounts = ...
// Order Total
$order_total = $order_subtotal + $order_shipping_total + $order_fees - $order_discounts;
订单总数将显示在已收到订单/致谢页面上。
如何获取用于计算订单总额的订单费用和折扣金额?
以下代码似乎不是正确答案:
$order_fees = $order->get_fees();
$order_discounts = $order->get_discount_total();
与您的预期相反,$order->get_fees()
不会 return 一个数字。这是因为结果是一个数组。
WC_Abstract_Order::get_fees()
– Return 此订单中的一系列费用。
所以你会得到:
$order_id = 2878;
// Get $order object
$order = wc_get_order( $order_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
$order_fees = 0;
// Get fees
foreach ( $order->get_fees() as $fee_id => $fee ) {
// Get total
$order_fees += $fee['line_total'];
// OR $order_fees += $fee->get_total();
}
echo $order_fees;
}
或者您的情况:
// Get subtotal
$order_subtotal = $order->get_subtotal();
// Get shipping total
$order_shipping_total = $order->get_shipping_total();
// Initialize
$order_fee_total = 0;
// Get fees
foreach ( $order->get_fees() as $fee_id => $fee ) {
$order_fee_total += $fee->get_total();
}
// Get discount total
$order_discount_total = $order->get_discount_total();
// Order Total
$order_total = $order_subtotal + $order_shipping_total + $order_fee_total - $order_discount_total;
基于
我知道如何获取购物车总数。计算中包括费用和折扣。
// Get cart subtotal & shipping total & fees & discounts
$subtotal = WC()->cart->subtotal;
$shipping_total = WC()->cart->get_shipping_total();
$fees = WC()->cart->get_fee_total();
$discount_excl_tax_total = WC()->cart->get_cart_discount_total();
$discount_tax_total = WC()->cart->get_cart_discount_tax_total();
$discount_total = $discount_excl_tax_total + $discount_tax_total;
// Cart Total
$cart_total = $subtotal + $shipping_total + $fees - $discount_total;
我还需要获取订单总数。我在获取订单折扣和费用时遇到问题。
function filter_woocommerce_get_order_item_totals( $order ) {
// Get order subtotal & shipping total & fees & discounts
$order_subtotal = $order->get_subtotal();
$order_shipping_total = $order->get_shipping_total();
$order_fees = ...
$order_discounts = ...
// Order Total
$order_total = $order_subtotal + $order_shipping_total + $order_fees - $order_discounts;
订单总数将显示在已收到订单/致谢页面上。
如何获取用于计算订单总额的订单费用和折扣金额?
以下代码似乎不是正确答案:
$order_fees = $order->get_fees();
$order_discounts = $order->get_discount_total();
与您的预期相反,$order->get_fees()
不会 return 一个数字。这是因为结果是一个数组。
WC_Abstract_Order::get_fees()
– Return 此订单中的一系列费用。
所以你会得到:
$order_id = 2878;
// Get $order object
$order = wc_get_order( $order_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
$order_fees = 0;
// Get fees
foreach ( $order->get_fees() as $fee_id => $fee ) {
// Get total
$order_fees += $fee['line_total'];
// OR $order_fees += $fee->get_total();
}
echo $order_fees;
}
或者您的情况:
// Get subtotal
$order_subtotal = $order->get_subtotal();
// Get shipping total
$order_shipping_total = $order->get_shipping_total();
// Initialize
$order_fee_total = 0;
// Get fees
foreach ( $order->get_fees() as $fee_id => $fee ) {
$order_fee_total += $fee->get_total();
}
// Get discount total
$order_discount_total = $order->get_discount_total();
// Order Total
$order_total = $order_subtotal + $order_shipping_total + $order_fee_total - $order_discount_total;