问题描述
我正在尝试以编程方式更改订单的运费。我在我的 woocommerce_order_status_processing
动作挂钩函数中尝试了类似于以下的代码。我更新了运费总额,但没有更新 FedEx 的实际订单项。我正在使用 pluginhive 的 FedEx 运输插件。有没有办法更新 FedEx 的价值和总数?
$shipping = $order->get_shipping_total();
$new_shipping = 1.00;
$order->set_shipping_total($new_shipping);
$order->save();
$order->calculate_shipping();
$order->calculate_totals();
解决方法
我最终使用了类似的东西(感谢@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();
可能不需要数量部分。