【发布时间】: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