'Add update or remove WooCommerce shipping order items
I have added shipping cost for the orders that are synced from amazon. For some reason I had to set custom shipping flat price in woo-orders created for amazon-order. It is done as follow:
$OrderOBJ = wc_get_order(2343);
$item = new WC_Order_Item_Shipping();
$new_ship_price = 10;
$shippingItem = $OrderOBJ->get_items('shipping');
$item->set_method_title( "Amazon shipping rate" );
$item->set_method_id( "amazon_flat_rate:17" );
$item->set_total( $new_ship_price );
$OrderOBJ->update_item( $item );
$OrderOBJ->calculate_totals();
$OrderOBJ->save()
The problem is, I have to update orders in each time the status is changed in amazon, there is no problem doing that, problem is I have to update the shipping cost also if it is updated. But I have not found anyway to do so. Can anyone tell me how to update the shipping items of orders set in this way ? Or is it the fact that, once shipping item is set then we cannot update or delete it ? Any kinds of suggestion are highly appreciated. Thanks.
Solution 1:[1]
The WC_Order_Item_Shipping object can be added to an order using either of 2 methods.
WC_ORDER->add_shipping( WC_Order_Item_Shipping )This is deprecated in WooCommerce V3.WC_ORDER->add_item( WC_Order_Item_Shipping )
If you need to persist this change on the database then use WC_ORDER->save();
References: woocommerce.github.io.../#add_shipping woocommerce.github.io.../#add_item
Solution 2:[2]
Just get order and delete item by id
$ordr_id = 4414;
$item_id = 986;
$order = wc_get_order($ordr_id);
$order->remove_item($item_id);
$order->calculate_totals();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Samuel Nwaokoro |
| Solution 2 | White Shot |
