最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce - update order item price and recalculate totals

programmeradmin1浏览0评论

What is the proper way to change order shipping item price and recalculate totals? I'm trying to do:

$order = new \WC_Order(2051);
foreach ($order->get_items('shipping') as $key => $item) {
    wc_update_order_item_meta($key,'cost',0);
    wc_update_order_item_meta($key,'total_tax',0);
    wc_update_order_item_meta($key,'taxes',[]);
}

$order->calculate_totals();

Which doesn't seem to work, and does not use WooCommerce datastore functions..

What is the proper way to change order shipping item price and recalculate totals? I'm trying to do:

$order = new \WC_Order(2051);
foreach ($order->get_items('shipping') as $key => $item) {
    wc_update_order_item_meta($key,'cost',0);
    wc_update_order_item_meta($key,'total_tax',0);
    wc_update_order_item_meta($key,'taxes',[]);
}

$order->calculate_totals();

Which doesn't seem to work, and does not use WooCommerce datastore functions..

Share Improve this question asked Feb 22, 2018 at 13:14 user1049961user1049961 4552 gold badges8 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Probably you already found a better way to do this since this question is quite old, but so far I could only make it work by getting the price difference manually and then using the method set_total from the order object:

$order = new \WC_Order(2051);
$diff = 0;
foreach ($order->get_items('shipping') as $key => $item) {
    wc_update_order_item_meta($key,'cost',0);
    wc_update_order_item_meta($key,'total_tax',0);
    wc_update_order_item_meta($key,'taxes',[]);
    $diff += $item->get_total();
}
$newTotal = $order->calculate_totals() - $diff;
$order->set_total($newTotal);
$order->save();

I will post this here because maybe it could be useful for someone. If you see this and you found a better / cleaner way to solve this I would like to see it :-).

发布评论

评论列表(0)

  1. 暂无评论