【问题标题】:Get order total without fees and discounts in Woocommerce在 Woocommerce 中获得无费用和折扣的订单总额
【发布时间】:2018-04-20 02:41:26
【问题描述】:

我已经使用 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' );

我想在用户提交订单后将其放入钩子中,这就是钩子

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
}

【问题讨论】:

  • 从未使用过 WooCommerce,也永远不会使用,但是文档告诉我购物车类中有用于该功能的功能:docs.woocommerce.com/wc-apidocs/class-WC_Cart.html 您是在谈论 order 总计还是 购物车总计?这里很困惑...
  • 不管是哪一个我都只想要总价而不像我说的那样增加费用,但最好是总价。
  • 给我们一些更多的信息,你想在订单创建之前还是之后获取?向我们提供您能想到的所有可能信息以及您尝试过的来源。
  • 我更新了我的问题,感谢您的建议。
  • 什么:get_order_item_totals 返回? docs.woocommerce.com/wc-apidocs/…

标签: php wordpress woocommerce orders


【解决方案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]} 操作挂钩:

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 文件中。经过测试并且可以工作。

【讨论】:

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