【问题标题】:Change order shipping total in WooCommerce在 WooCommerce 中更改订单运费总额
【发布时间】:2021-05-28 12:10:31
【问题描述】:

我正在尝试以编程方式更改订单的运费。我在woocommerce_order_status_processing 操作挂钩函数中尝试了类似于以下的代码。我得到了要更新的运输总额,但不是 FedEx 的实际订单项。我正在使用 pluginhive 的 FedEx shipping 插件。有没有办法更新 FedEx 的值和总数?

$shipping = $order->get_shipping_total();
$new_shipping = 1.00;
$order->set_shipping_total($new_shipping);
$order->save();


$order->calculate_shipping();
$order->calculate_totals();

【问题讨论】:

  • 您需要改写相关的订单“运输”项目(通过克隆它并更改克隆项目的数量,删除原始并保存克隆的项目)......然后你会能够使用calculate_shipping()calculate_totals() 方法。
  • 谢谢@LoicTheAztec。我使用 $order->get_items('shipping') 发布了我的答案。我能够在不克隆的情况下使其工作(我不确定你的意思)。 $qty 部分可能不需要。该代码用于订购商品,并且它们都共享一个功能,因为其中一些代码是重复的。
  • 是的,这是正确的方法,请参阅:Add update or remove WooCommerce shipping order items

标签: php wordpress woocommerce orders shipping


【解决方案1】:

我最终使用了类似 的东西(感谢@LoicTheAztec 的帮助)

foreach( $order->get_items( 'shipping' ) as $item_id => $item ) {
    $item_price = $item->get_total();
    $qty = (int) $item->get_quantity();
    $item_price = ($item_price / $exchange_rate) * $qty;

    $item->set_total( $item_price );
    $item->calculate_taxes();
    $item->save();
}

$order->calculate_shipping();
$order->calculate_totals();

数量部分可能不需要。

【讨论】:

  • 是的,您可以删除不需要的数量……
猜你喜欢
  • 2021-12-13
  • 2019-09-12
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 2017-03-14
  • 2021-11-23
  • 2021-03-09
  • 2022-01-22
相关资源
最近更新 更多