【问题标题】:Removing shipping total from order total in WooCommerce从 WooCommerce 中的订单总额中删除运费总额
【发布时间】:2021-12-13 12:30:39
【问题描述】:

我正在尝试从购物车总数中删除运费,我已使用以下代码在购物车和结帐页面上进行处理。

但是,在“订单确认”页面上,配送并未被删除。

为购物车和结帐工作

add_action( 'woocommerce_after_calculate_totals', 'woocommerce_after_calculate_totals', 110 );
function woocommerce_after_calculate_totals( $cart ) {
     // make magic happen here...
    // use $cart object to set or calculate anything.
    ## Get The shipping totals
    $shipping = WC()->cart->get_shipping_total();
    $totalincshipping = $cart->total;
    $cart->total = $totalincshipping-$shipping;

}

我在 ORDER CONFIRMATION 页面尝试了以下操作,但这并没有得到预期的结果:

add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 100, 2 );
function change_total_on_checking( $order ) {
    // Get order total
    $shipping = $order->get_shipping_total();
    $total = $order->get_total();
    ## -- Make your checking and calculations -- ##
    $new_total = $total - "10"; // <== Fake calculation
    // Set the new calculated total
    $order->set_total( $new_total );
}

有什么建议吗?

【问题讨论】:

  • 是“收到订单”页面吗?
  • @bossman 是的

标签: wordpress woocommerce cart orders shipping


【解决方案1】:

您可以只使用woocommerce_calculated_total 过滤钩子,它允许插件过滤总计,并在修改时汇总购物车总计。

// Allow plugins to filter the grand total, and sum the cart totals in case of modifications.
function filter_woocommerce_calculated_total( $total, $cart ) {
    // Get shipping total
    $shipping_total = $cart->get_shipping_total();
    
    return $total - $shipping_total;
}
add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 10, 2 );

无需进一步调整或附加代码,因为购物车总数将被保存/存储并自动用于其他页面


更新

由于您使用的是Bayna – Deposits & Partial Payments for WooCommerce插件,这可能与我的回答有冲突,因为您使用的插件包含相同的过滤器挂钩

即在 1.2.1 版第 15 行的\deposits-for-woocommerce\src\Cart.php 文件中

add_filter( 'woocommerce_calculated_total', [$this, 'recalculate_price'], 100, 2 );

所以为了让我的答案代码在这个钩子之后运行,把它的优先级改为 100+

替换我的答案

add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 10, 2 );

add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 110, 2 );

致谢:Bossman

【讨论】:

  • 谢谢,我试过了,它适用于购物车和结帐,但在收到订单页面上,运费被添加到总金额中。我正在使用 Banya 存款插件,我认为这可能会造成干扰 - 但仅限于“已收到订单”页面。
  • 也许尝试使用add_filter 优先级,因为另一个插件可能也在使用它。 add_filter( 'woocommerce_calculated_total', 'filter_woocommerce_calculated_total', 50, 2 ); 之类的东西值得一试。更好的是,在插件代码库中搜索该过滤器,看看它是否正在使用它..
  • 我查看了插件源,怀疑他们正在使用这个add_filter( 'woocommerce_calculated_total', [$this, 'recalculate_price'], 100, 2 );,所以请优先考虑200,它将在插件过滤器之后执行..
  • 谢谢@7uc1f3r,但不幸的是,即使在更改优先级之后(我尝试了 50、110 和 200),我也遇到了同样的问题,它适用于购物车和结帐,但在订单确认时,计算似乎以不同的顺序完成,然后在计算存款之前将运费添加回计算中。 Orders 仪表板中的情况也是如此,尽管有点混淆了两个“_due_payments”金额,一个不包括运费,一个包含运费。
  • @LeeBaz 那么您必须通过他们的支持页面将您的问题直接发送给插件开发人员,因为我的回答通常有效
猜你喜欢
  • 2021-05-28
  • 1970-01-01
  • 2016-02-18
  • 2021-11-23
  • 2017-03-14
  • 2019-09-12
  • 2022-01-22
  • 1970-01-01
  • 2013-01-13
相关资源
最近更新 更多