【问题标题】:Get order fee and discount amounts for order totals calculations in WooCommerce在 WooCommerce 中获取订单总额计算的订单费用和折扣金额
【发布时间】:2021-11-23 03:22:01
【问题描述】:

基于Calculate fee from cart totals (subtotal + shipping) without adding it to order total value 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();

【问题讨论】:

    标签: php wordpress woocommerce orders fee


    【解决方案1】:

    与您的预期相反,$order->get_fees() 不会返回数字。这是因为结果是一个数组。

    WC_Abstract_Order::get_fees() – 返回此订单中的一系列费用。

    所以你会得到:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2021-05-27
      • 2021-12-13
      • 2021-05-28
      • 1970-01-01
      相关资源
      最近更新 更多