dtmm0148603 2018-04-20 02:41
浏览 64
已采纳

在Woocommerce中获得没有费用和折扣的订单总额

Hi i have dynamically added some negative fees to woocommerce cart using

$woocommerce->cart->add_fee(); function. Now i want to get order total price

from WC_Order() class without counting any fee or even discounts and shipping fee.

EDIT

I don't know why i got down votes but i searched everywhere a lot and found nothing.

I tested WC_Order()::get_total() but it return zero if i the total price be $10 and i added a negative fee of $15 like this

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

EDIT 2

I want to get it in a hook after user submit order this is the hook

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条回答 默认 最新

  • doucong8553 2018-04-20 12:21
    关注

    First, your code is a little outdated for the cart negative fee part:

    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.

    To get the order gran total excluding all discounts you will use the following in your function:

    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
    }
    

    Alternatively you can use woocommerce_order_status_{$status_transition[to]} action hook on "processing" and "completed" paid statuses:

    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
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么