Search code examples
magentomagento-1.5

Cannot saveShippingMethod in Magento One Page Checkout


I am writing a custom Magento 1.5.0.1 one page checkout for my site following the instructions on this site: http://inchoo.net/ecommerce/magento/magentos-onepage-checkout-in-a-nutshell/

In summary, I am calling these functions in order:

$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 is that when the code is first run, the shipping method is not being set, I get a error saying that the shipping method was not set. However, just refreshing the page makes the order go through.

One solution was that right after setting the shipping method with saveShippingMethod, checking if it was set with:

Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->getShippingMethod();

Which 100% of the time it is not, then redirecting it back onto the same page, which on the 2nd run the shipping method is set...

this seems to be such a stupid magento bug! any ideas on how to fix it with this redirection (i.e. page refresh)?


Solution

  • Maybe it is me not being extremely expert about magento, but I'm pretty sure you have to use setShippingMethod($method) instead of saveShippingMethod($method) when creating an order, you can check more here.