1
votes

I am trying to create a custom module for MAGENTO CHECKOUT in which I want to remove SHIPPING ADDRESS AND SHIPPING METHOD steps.

So my code looks like below:

app/etc/Mona_Test.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Mona_Test>
      <active>true</active>
      <codePool>local</codePool>
    </Mona_Test>
   </modules>
</config>

app/code/local/Mona/Test/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Mona_Test>
        <version>1.0</version>
    </Mona_Test>
  </modules>
  <global>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <Mona_Test before="Mage_Checkout">Mona_Test</Mona_Test>    
                    </modules>    
                </args>
            </checkout>
        </routers>
    </frontend>

    <blocks>
        <test>
            <class>Mona_Test_Block</class>
        </test>
        <checkout>
            <rewrite>
                <onepage_abstract>Mona_Test_Block_Onepage_Abstract</onepage_abstract>
            </rewrite>
        </checkout>
    </blocks>

    <models>
        <test>
            <class>Mona_Test_Model</class>
        </test>
        <checkout>
            <rewrite>
                <type_onepage>Mona_Test_Model_Type_Onepage</type_onepage>
            </rewrite>
        </checkout>
    </models>
  </global>
</config>

app/code/local/Mona/Test/Blocks/Onepage/Abstract.php

 <?php  
 class Mona_Test_Block_Onepage_Abstract extends Mage_Checkout_Block_Onepage_Abstract {
protected function _getStepCodes()
{
    return array('login', 'billing', 'payment', 'review');
}
}  

?>

app/code/local/Mona/Test/controllers/OnepageController.php

<?php
 require_once 'Mage/Checkout/controllers/OnepageController.php';
  class Mona_Test_OnepageController extends Mage_Checkout_OnepageController
 {
    protected $_sectionUpdateFunctions = array(
      'payment-method' => '_getPaymentMethodsHtml',
      'review' => '_getReviewHtml',
    );

public function saveBillingAction()
{
    if ($this->_expireAjax()) {
        return;
    }
    $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()
            );
        } else {
            $result['goto_section'] = 'payment';
        }
    }
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}

public function saveShippingAction()
{
    if ($this->_expireAjax()) {
        return;
    }
    if ($this->getRequest()->isPost()) {
        $data = $this->getRequest()->getPost('shipping', array());
        $customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
        $result = $this->getOnepage()->saveShipping($data, $customerAddressId);

        if (!isset($result['error'])) {
            $result['goto_section'] = 'payment';
            $result['update_section'] = array(
                'name' => 'payment-method',
                'html' => $this->_getShippingMethodsHtml()
            );
        }
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
    }
}

}

app/code/local/Mona/Test/Model/Type/Onepage.php

<?php
class Mona_Test_Model_Type_Onepage extends Mage_Checkout_Model_Type_Onepage
{
    public function saveShippingMethod($shippingMethod)
    {
        if (empty($shippingMethod)) {
            $shippingMethod = 'freeshipping_freeshipping';
        }
        $rate = $this->getQuote()->getShippingAddress()->getShippingRateByCode($shippingMethod);

        if (!$rate) {
            return array('error' => -1, 'message' => Mage::helper('checkout')->__('Invalid shipping method.'));
        }

        $this->getQuote()->getShippingAddress()
            ->setShippingMethod($shippingMethod);

        $this->getCheckout()
            ->setStepData('shipping_method', 'complete', true)
            ->setStepData('payment', 'allow', true);

        return array();
    }
}

?>

Its not working. what more should i do? Any solutions to it will be greatful.

1
What exactly do you mean by Its not working. ?Flakes
It means the default checkout is working, but not my custom module.Monalisha
Did @SearchAndResQ get what I am trying to say?Monalisha

1 Answers

2
votes
Go to:
**app\code\core\Mage\Checkout\Block \Onepage.php**
Change the code :
public function getSteps(){
 $steps = array();
 if (!$this->isCustomerLoggedIn()) {
 $steps['login'] = $this->getCheckout()->getStepData('login');
 }
 //$stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'review');
 $stepCodes = array('billing', 'payment', 'review');
 foreach ($stepCodes as $step) {
 $steps[$step] = $this->getCheckout()->getStepData($step);
 }
 return $steps;
 }


Go to:
app\code\core\Mage\Checkout\controllers\ OnepageController.php

Edit:
protected $_sectionUpdateFunctions = array(
 'payment-method' => '_getPaymentMethodsHtml',
 // 'shipping-method' => '_getShippingMethodsHtml',
 'review' => '_getReviewHtml',
 );

 Also edit  saveBillingAction() function

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'] = 'shipping_method';
 $result['update_section'] = array(
 'name' => 'shipping-method',
 'html' => $this->_getShippingMethodsHtml()
 );
 $result['allow_sections'] = array('shipping');
 $result['duplicateBillingInfo'] = 'true';
 }*/
 //End of Comment by Amit Bera
 else {
 //$result['goto_section'] = 'shipping';
 $result['goto_section'] = 'payment';
 }
 }
 $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
 }
 }

 Go to:
\app\code\core\Mage\Sales\Model\Service\ Quote.php

Edit:


protected function _validate()
 {
 $helper = Mage::helper('sales');
 if (!$this->getQuote()->isVirtual()) {
 $address = $this->getQuote()->getShippingAddress();
 $addressValidation = $address->validate();
 // if ($addressValidation !== true) {
 // Mage::throwException(
 //$helper->__('Please check shipping address information. %s', implode(' ', $addressValidation))
 //);
 //}
 // $method= $address->getShippingMethod();
 //$rate = $address->getShippingRateByCode($method);
 //if (!$this->getQuote()->isVirtual() && (!$method || !$rate)) {
 //Mage::throwException($helper->__('Please specify a shipping method.'));
 //}
 }

In billing information tab you see radio button for shipping address as billing address ,just hidden it or remove it .the file located at – app\design\frontend\default\your template\yourtemplate\ persistent\checkout\onepage\billing,phtml Or app\design\frontend\default\your template\template \checkout\onepage\billing.phtml

In  app\locale\en_US\template\email\sales\ order_new.html
From
{{var order.getShippingAddress().format('html')}}
To
{{var order.getBillingAddress().format('html')}}
And
{{var order.getShippingDescription()}}
Remove it.  

you can also refer below link

http://www.bluehorse.in/blog/removed-shipping-address-and-shipping-method-from-onepage-checkout-in-magento.html