Jordan Carter
Jordan Carter

Reputation: 1336

Change order shipping total in WooCommerce

I'm trying to programmatically change the shipping price for an order. I've tried code similar to the following in my woocommerce_order_status_processing action hook function. I got the shipping total to update, but not the actual line item for FedEx. I'm using pluginhive's FedEx shipping plugin. Is there a way to update the FedEx value as well as the total?

$shipping = $order->get_shipping_total();
$new_shipping = 1.00;
$order->set_shipping_total($new_shipping);
$order->save();


$order->calculate_shipping();
$order->calculate_totals();

Upvotes: 1

Views: 2261

Answers (1)

Jordan Carter
Jordan Carter

Reputation: 1336

I ended up using something like (thanks to @LoicTheAztec for the assistance):

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();

The quantity part may not be needed.

Upvotes: 1

Related Questions