Search code examples
magentomagento-1.4magento-1.5magento-1.7magento-1.6

How to auto fill shipping method Magento for onepagechekout


I want to auto fill the shipping method automatically and not show it on the onepage checkout. I was able to hide the shipping method on my Onepagechekout by changing this on app/code/local/Mage/Checkout/controllers/OnepageController.php:

/**
 * save checkout billing address
 */
public function saveBillingAction()
{
    if ($this->_expireAjax()) {
        return;
    }
    if ($this->getRequest()->isPost()) {
//            $postData = $this->getRequest()->getPost('billing', array());
//            $data = $this->_filterPostData($postData);
        $data = $this->getRequest()->getPost('billing', array());
        $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);

        if (isset($data['email'])) {
            $data['email'] = trim($data['email']);
        }
        $result = $this->getOnepage()->saveBilling($data, $customerAddressId);

        if (!isset($result['error'])) {
            /* check quote for virtual */
            if ($this->getOnepage()->getQuote()->isVirtual()) 
            {
                $result['goto_section'] = 'payment';
                $result['update_section'] = array(
                    'name' => 'payment-method',
                    'html' => $this->_getPaymentMethodsHtml()
                );
            } elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) 
              {
                $result['goto_section'] = 'payment';
                $result['update_section'] = array(
                    'name' => 'payment-method',
                    'html' => $this->_getPaymentMethodsHtml()
                );

                $result['allow_sections'] = array('shipping');
                $result['duplicateBillingInfo'] = 'true';
            } else {
                $result['goto_section'] = 'shipping';
            }
        }
        $this->saveShippingMethodAction();
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
    }
}

As you can see I changed the link where I redirect the next step after billing action to the payment step.

In order to auto-save a shipping method, I added

$this->saveShippingMethodAction();

at the end of the function, and this method looks like here:

    public function saveShippingMethodAction()
{
    $this->_expireAjax();
    if ($this->getRequest()->isPost()) {
        /* $this->savePaymentAction(); */
        $data = $this->getRequest()->getPost('shipping_method', 'flatrate_flatrate');
        $result = $this->getOnepage()->saveShippingMethod($data);
           $this->getResponse()->setBody(Zend_Json::encode($result));
    }

}

So what I did is try to include automatically flatrate_flatrate method as the default one.

But when I try to finish a sale, it says I didn't specify a shipping method. Any idea on why it doesn't work?


Solution

  • Now you need to force the shipping_method in the quote and in the session :

    $forcedMethod = "your_method_code";
    
    $this->getOnePage()->getQuote()
        ->getShippingAddress()
        ->setShippingMethod($forcedMethod)
        ->save();
    
    Mage::getSingleton('checkout/session')->setShippingMethod($forcedMethod);
    

    And just before you call :

    $this->saveShippingMethodAction(); 
    

    add a :

    $this->getRequest()->setPost('shipping_method', $forcedMethod);
    

    It should work ;)