微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 WooCommerce 中更改订单运输总额

如何解决在 WooCommerce 中更改订单运输总额

我正在尝试以编程方式更改订单的运费。我在我的 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();

可能不需要数量部分。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。