Programmatically change Magento shipping method in existing order

Programmatically change Magento shipping method in existing order

August 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Programmatically change Magento shipping method in existing order

Programmatically Changing Magento Shipping Method in Existing Orders

As a senior developer working with Magento, you may find yourself needing to programmatically change the shipping method on existing orders from time to time. This could be due to changes in carrier rates, order retries, or other business logic requirements. In this post, we’ll delve into how to achieve this using Magento’s built-in API.

Understanding the Issue with the Current Approach

The initial code snippet provided attempts to set the shipping method directly on an existing order:

$order = Mage::getModel('sales/order')->load($orderId);
$order->getShippingAddress()->setShippingMethod('shipping_method');
$order->save();

However, as you’ve discovered, this approach doesn’t always work as expected. The issue lies in the way Magento handles shipping method calculations and storage.

Digging Deeper into Magento Shipping Method Storage

Magento stores shipping methods in two places:

  1. The order’s shipping_address table: This contains the selected shipping method for the order.
  2. The order’s shipping_methods collection: This contains a list of available shipping methods and their rates that were calculated at the time of the quote.

When you call $order->getShippingMethod(), Magento reads from both these places to determine the current shipping method.

The Proper Way to Change Shipping Method

To change the shipping method correctly, you need to perform two additional steps:

  1. Delete the existing shipping methods collection: Before setting a new shipping method, delete the existing shipping_methods collection associated with the order’s quote.

  2. Re-collect the shipping methods and set the desired one: After deleting the collection, re-collect all available shipping methods for the order’s quote. From this fresh list, you can then set your desired shipping method.

Here’s how to implement this in code:

$order = Mage::getModel('sales/order')->load($orderId);

// Delete existing shipping methods
$order->getQuote()->collectShippingRates();
$order->setShippingMethod(null);
$order->save();

// Set new shipping method
$order->getQuote()->getShippingAddress()->setShippingMethod('shipping_method');
$order->getQuote()->collectTotals();
$order->save();

Note that we’re using the $order->getQuote() object to interact with the order’s quote. The collectShippingRates method deletes the existing shipping methods collection, and setShippingMethod sets the new method on the fresh list collected during collectTotals.

Handling Special Cases: Fixed Shipping Rates

If your new shipping method is not a standard Magento carrier or has fixed rates that don’t depend on product data, you’ll need to manually set the rate as well. You can do this by adding the following code after setting the shipping method:

$rate = Mage::getModel('shipping/rate_result_method');
$rate->setCarrierTitle($methodName);
$rate->setMethodTitle($newRate);
$rate->setAmount($fixedRate);

$order->getQuote()->addShippingRate($rate);

Here, we’re creating a new rate_result_method object with the carrier title, method title, and amount. We then add this rate to the order’s quote using the addShippingRate method.

Conclusion

Programmatically changing the shipping method on existing orders in Magento requires a deeper understanding of how the system stores and calculates shipping rates. The key is to delete the existing shipping methods collection, re-collect fresh rates, and then set your desired method from this updated list.

Remember, any changes made programmatically should be thoroughly tested to ensure they don’t introduce unexpected behavior or errors. Always consider using Magento’s built-in order management functionality where possible to avoid complex customizations.

For more advanced Magento development needs, consider reaching out to Laravel Company, a leading provider of Magento development services and solutions. They offer expertise in everything from theme customization to custom extension development and integration.

Happy coding!

Database API Orders Errors

Our network