在Woocommerce

时间:2018-04-20 02:41:27

标签: php wordpress woocommerce orders totals

您好我已经使用

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

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

来自WC_Order()课程,不计算任何费用甚至折扣和运费。

修改

我不知道为什么我得到了选票,但我到处搜寻了很多,却一无所获。

我测试了WC_Order()::get_total(),但如果我的总价格为10美元并且我加了15美元的负费用,它就会返回零

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

编辑2

我希望在用户提交订单之后将其放入钩子中这是钩子

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
}

1 个答案:

答案 0 :(得分:1)

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

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

现在你需要知道的是,负费用总是适用税。

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

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
}

或者,您可以使用woocommerce_order_status_{$status_transition[to]}操作挂钩"处理"和#34;完成" 付费状态:

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文件中。经过测试和工作。