Solving the Shipping Method Bug in Magento One Page Checkout
The Problem: Inconsistent Shipping Method Saving
If you’re developing a custom one page checkout for your Magento site following guides like the one on Inchoo, you might be facing an issue where the shipping method isn’t being set correctly the first time around. This leads to errors stating that the shipping method was not set, even though all other steps seem to be working fine.
The issue lies in the sequential execution of functions responsible for saving the checkout data:
$checkout = Mage::getSingleton('checkout/type_onepage');
$checkout->saveCheckoutMethod('guest');
$checkout->saveBilling($billingAddress, false);
$checkout->saveShipping($shippingAddress, false);
$checkout->saveShippingMethod('flatrate_flatrate');
$checkout->savePayment(array('method' => 'checkmo'));
// Extra part not on the site but saw it in the original magento onpage checkout controller
$checkout->getQuote()->getPayment()->importData(array('method' => 'checkmo'));
//
$checkout->saveOrder();
// Extra part not on the site but saw it in the original magento onpage checkout controller
$checkout->getQuote()->save();
//
The problem arises when you try to retrieve the shipping method immediately after setting it using saveShippingMethod():
Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->getShippingMethod();
This almost always returns an empty string, indicating that the method hasn’t been set yet.
The Temporary Workaround: Redirecting and Refreshing
One quick fix that developers often resort to is redirecting back to the same page after setting the shipping method. This forces a page refresh, which seems to trigger the proper saving of the shipping method:
// After $checkout->saveShippingMethod('flatrate_flatrate');
Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->getShippingMethod();
if (empty($shippingMethod)) {
// Redirect back to the checkout page
Mage_Core_Controller_Front_Action::redirect(Mage::app()->getRequest(), '/checkout/onepage');
}
While this works, it’s a band-aid solution that relies on user action (the refresh) to correct the bug. It also introduces potential UI/UX issues.
The Permanent Solution: Deferring the Save
To solve this issue permanently and cleanly without relying on redirects or page refreshes, you need to understand why the shipping method isn’t being saved correctly when called sequentially.
The problem is that Magento’s one page checkout saves data incrementally during each function call. When saveShippingMethod() is called, it doesn’t immediately save the new method; instead, it marks the quote as needing a full save. The actual saving occurs when you call saveOrder().
To fix this without resorting to redirects or extra calls to getQuote()->save(), we need to ensure that Magento has fully processed all the previous data before setting the shipping method:
// Save billing and shipping addresses first
$checkout->saveBilling($billingAddress, false);
$checkout->saveShipping($shippingAddress, false);
// Then perform a full quote save
$checkout->getQuote()->save();
// Now set the shipping method (this should work)
$checkout->saveShippingMethod('flatrate_flatrate');
// Proceed with saving payment and order as normal
By forcing a save() on the quote before setting the shipping method, you ensure that all necessary data is fully processed before Magento tries to apply the new shipping method. This should resolve the issue consistently without needing any redirects or workarounds.
Conclusion
Inconsistent behavior when saving shipping methods in Magento’s one page checkout can be a frustrating bug to troubleshoot. However, by understanding the sequential nature of data saving and properly leveraging Magento’s quote handling functions, you can implement a clean, permanent solution that doesn’t rely on redirects or refreshes.
Remember, always ensure your Magento setup is up-to-date and consider using a reputable extension like Laravel Company for one page checkout functionality to avoid such issues. Proper documentation and support can save you countless hours of debugging time.
Happy coding!