在 Woocommerce 中免费获取订单总额和折扣

Get order total without fees and discounts in Woocommerce

我使用

向 woocommerce 购物车动态添加了一些负费用

$woocommerce->cart->add_fee(); 函数。现在我想得到订单总价

WC_Order() class 不计算任何费用甚至折扣和运费。

我测试了 WC_Order()::get_total(),但如果我的总价是 10 美元并且我像这样添加了 15 美元的负费用,它 return 为零

function add_custom_fee() {
     global $woocommerce;
     $woocommerce->cart->add_fee(__('Custom', 'woocommerce'), -15);
}
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee' );

我想在用户提交订单后挂钩,这就是挂钩

add_action('woocommerce_payment_complete','myfunc');

function myfunc($order_id) {
    $order = new WC_Order( $order_id );
    $customer = $order->get_customer_id();
    $price = $order->get_total(); // this return zero if i added fee to order
}

首先,您的代码对于购物车负费用部分有点过时:

add_action( 'woocommerce_cart_calculate_fees', 'custom_flat_discount', 20, 1 );
function custom_flat_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart->add_fee( __("Discount", "woocommerce"), -15 );
}

Now what you have to know is that a negative fee always apply taxes.

要获得排除所有折扣的订单总计,您将在函数中使用以下内容:

add_action( 'woocommerce_payment_complete', 'action_payment_complete', 20, 1 );
function action_payment_complete( $order_id ) {
    // get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    // Initialising variables
    $subtotal = $subtotal_taxes = 0; 

    // Get order items subtotal and subtotal tax
    foreach( $order->get_items() as $item ){
        $subtotal += (double) $item->get_subtotal();
        $subtotal_taxes += (double) $item->get_subtotal_tax();
    }
    // Order subtotal without any discounts, shipping…
    $order_subtotal = $subtotal + $subtotal_taxes;

    // Get order shipping totals
    $shipping_total = $order->get_shipping_total();
    $shipping_total_tax = $order->get_shipping_tax();

    // Order subtotal with shipping but without any discounts
    $order_subtotal_with_shipping = round($order_subtotal + $shipping_total + $shipping_total_tax, 2);

    // Your other code goes here
}

或者,您可以在 "processing" 和 "completed" paid 状态上使用 woocommerce_order_status_{$status_transition[to]} 操作挂钩:

add_action( 'woocommerce_order_status_processing', 'action_order_status_completed', 20, 2 );
add_action( 'woocommerce_order_status_completed', 'action_order_status_completed', 20, 2 );
function action_order_status_completed( $order_id, $order ) {
    // Initialising variables
    $subtotal = $subtotal_taxes = 0; 

    // Get order items subtotal and subtotal tax
    foreach( $order->get_items() as $item ){
        $subtotal += (double) $item->get_subtotal();
        $subtotal_taxes += (double) $item->get_subtotal_tax();
    }
    // Order subtotal without any discounts, shipping…
    $order_subtotal = $subtotal + $subtotal_taxes;

    // Get order shipping totals
    $shipping_total = $order->get_shipping_total();
    $shipping_total_tax = $order->get_shipping_tax();

    // Order subtotal with shipping but without any discounts
    $order_subtotal_with_shipping = round($order_subtotal + $shipping_total + $shipping_total_tax, 2);

    // Your other code goes here
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。