Christoffer Bubach Christoffer Bubach - 1 year ago 151
PHP Question

Magento: change shipping method on existing order

I'm trying to change the shipping on an existing order in Magento. This works fine from the admin backend, even if it's quite the process since I have to manually update a lot of the order fields/attributes after I set the new shipping method on the shipping address object and recalculate the quote totals.

My problem is when running the same code on the frontend, it doesn't work at all, the quote collectTotals will revert any changes I've made in the shippingAddress, and I have no idea how to solve it or why it works from the backend.

This is how it looked:

$shippingAddress = $quote->getShippingAddress();





And as I said, that worked fine every time from the backend, but not when called from the frontend.

I've tried variations, such as this to try and eradicate any trace of the old shipping method, with no luck.


It looks to me as if the quote is using an older/diffrent copy of the shipping address when I'm calling collectTotals, no matter what I do.

Any suggestions, or perhaps insight on how it's even possible that this works in the backend but not the frontend?


After more debugging, I can see that the shipping does change both in frontend and backend. The problem is, the fee will only change when running this code through the backend. Very strange. It just refuses to update shipping fee.

Answer Source

Looks like I had some issues with an observer on collectTotals, which is the reason it worked in the backend where the event wasn't fired.

The complete code for reference, which I recently changed to use a more fail-safe method to copy all the fields back to the order.

    /* @var $order Mage_Sales_Model_Order */
    /* @var $quote Mage_Sales_Model_Quote */

    $shippingAddress = $quote->getShippingAddress();


    if ($this->updateMagentoOrder($order, $quote)) {

        // here's where I check if we successfully updated the authorized
        // amount at the payment gateway, before saving anything
        // wrapping the payment update and save in a try-catch


And using this method for updating all the order fields:

 * Updates a Magento order based on quote changes
 * will not save anything, up to the caller.
 * deleting items not supported.
 * @param  $order Mage_Sales_Model_Order
 * @param  $quote Mage_Sales_Model_Quote
 * @return bool
public function updateMagentoOrder($order, $quote) {
    if (!$order instanceof Mage_Sales_Model_Order || !$quote instanceof Mage_Sales_Model_Quote) {
        return false;

    try {
        $converter = Mage::getSingleton('sales/convert_quote');
        $converter->toOrder($quote, $order);

        foreach ($quote->getAllItems() as $quoteItem) {

            $orderItem     = $converter->itemToOrderItem($quoteItem);
            $quoteItemId   = $quoteItem->getId();
            $origOrderItem = empty($quoteItemId) ? null : $order->getItemByQuoteItemId($quoteItemId);

            if ($origOrderItem) {
            } else {
                if ($quoteItem->getParentItem()) {

        if ($shippingAddress = $quote->getShippingAddress()) {
            $converter->addressToOrder($shippingAddress, $order);
    } catch (Exception $e) {
        return false;

    return true;

For reference, the method above could loop $order->getAllItems() and do $orderItem->cancel()->delete(); on them first - but I won't support deleting items right now.

The cancel() part before deletion is so that the CatalogInventory module can restore stock. It's listening for the sales_order_item_cancel event.