I have a site with woocommerce and several other plugins (Role Based Methods in particular)
Each user role has two shipping methods: a paid one (the price changes according to the role) and a free one (with a min_amount which also changes according to the role)
During a specific action by the administrator on an order (or an order batch), an update of the prices of the items is done and I wish to update the shipping method (only in the case where the user had a paid method and the new rates exceed the min_amount of his free method)
I manage to retrieve the order total and the shipping methods available for the role of the user
But I can’t update the shipping method.
In fact here is the var_dump of $order->get_items(‘shipping’)when it’s free shipping:
And the var_dump when isn’t free shipping
as you can see, the shipping method name and price is in the “data” table
So I tried this code:
foreach ($actualShipping as $item_id => $item){
$data = $item->get_data();
$data["instance_id"] = $freeMethodForUser[0]["instance_id"];
$data["name"] = $freeMethodForUser[0]["method_title"];
$data["method_title"] = $freeMethodForUser[0]["method_title"];
$data["method_id"] = $freeMethodForUser[0]["id"];
$data["total"] = "0.00";
$item->set_props(array(
'method_title' => $item->get_method_title(),
'method_id' => $item->get_method_id(),
'total' => wc_format_decimal($item->get_total()),
'taxes' => $item->get_total_tax(),
'meta_data' => $item->get_meta_data(),
'data' => $data)
);
$item->save();
}
$order->calculate_totals();
$order->save();
$freeMethodForUser[0] is an object of type WC_Shipping_Free_Shipping
Before the $item->set_props if I put a var_dump of $data, it’s okay my new shipping method is here
But I can’t save it
Could you please help me to update my shipping method ?