Magento – Custom extension adding extra step in the onepage checkout page

ce-1.9.1.0checkoutonepage-checkoutPHP

I am using magento 1.9.1.

I am working on a extensions which is allowing the users to pay part of they order, everything is cool and i've done everything what i wanted so far.

The next step that i should do is to create an extra step in the checkout onepage.

My extensions is located like that: /app/code/local/Vivas/PercentPayment/and here are all folders and files of the extension

My questions is how i can create new step in the onepage checkout from the extension. I mean i want to create the new extra step in the checkout's onepage from my extension files. Is that possible and how ?

Thanks in advance!

Best Answer

By default magento gives some checkout steps. But Sometime you need to add extra information from the customer for future reference. A common requested customization is to add the Custom Form in default checkout process. This is not good practice to touch core files. You can do this via overriding Modules. In this example Comapnyname is Ipragmatech and Module name is Checkoutstep.

Step1: Add Custom step in the checkout process

Open the Ipragmatech/Checkoutstep/Block/Onepage/Checkoutstep.php file and write the following code:

class Ipragmatech_Checkoutstep_Block_Onepage_Checkoutstep extends Mage_Checkout_Block_Onepage_Abstract
{
    protected function _construct()
    {
        $this->getCheckout()->setStepData('checkoutstep', array(
            'label' => Mage::helper('checkout')->__('Invitation to participation'),
            'is_show' => true
        ));
        parent::_construct();
    }
}

Step2: Add steps which and where you want in the checkout process

Open the Ipragmatech/Checkoutstep/Block/Onepage/Checkoutstep.php file and write the following code:

class Ipragmatech_Checkoutstep_Block_Onepage extends Mage_Checkout_Block_Onepage
{
    public function getSteps()
    {
        $steps = array();

        if (!$this->isCustomerLoggedIn()) {
            $steps['login'] = $this->getCheckout()->getStepData('login');
        }

        $stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'checkoutstep', 'review');
        foreach ($stepCodes as $step) {
            $steps[$step] = $this->getCheckout()->getStepData($step);
        }

        return $steps;
    }
}

Step3: Grab the submitted value of custom form and set the values of Custom form.

Open the Ipragmatech/Checkoutstep/controllers/OnepageController.php and write the following function:

public function saveCheckoutstepAction()
{
    $this->_expireAjax();
    if ($this->getRequest()->isPost()) {

        //Grab the submited value 
        $_entrant_name = $this->getRequest()->getPost('entrant_name', "");
        $_entrant_phone = $this->getRequest()->getPost('entrant_phone', "");
        $_entrant_email = $this->getRequest()->getPost('entrant_email', "");
        $_permanent_address = $this->getRequest()->getPost('permanent_address', "");
        $_address = $this->getRequest()->getPost('local_address', "");

        Mage::getSingleton('core/session')->setIpragmatechCheckoutstep(serialize(array(
            'entrant_name' => $_entrant_name,
            'entrant_phone' => $_entrant_phone,
            'entrant_email' => $_entrant_email,
            'permanent_address' => $_permanent_address,
            'address' => $_address
        )));

        $result = array();
        $redirectUrl = $this->getOnePage()->getQuote()->getPayment()->getCheckoutRedirectUrl();
        if (!$redirectUrl) {
            $this->loadLayout('checkout_onepage_review');
            $result['goto_section'] = 'review';
            $result['update_section'] = array(
                'name' => 'review',
                'html' => $this->_getReviewHtml()
            );

        }

        if ($redirectUrl) {
            $result['redirect'] = $redirectUrl;
        }

        $this->getResponse()->setBody(Zend_Json::encode($result));
    }
}

Step4: Save Custom Form information

When checkout_onepage_controller_success_action event hook is called. Open the Ipragmatech/Checkoutstep/Model/Observer.php and write the following:

class Ipragmatech_Checkoutstep_Model_Observer
{
    const ORDER_ATTRIBUTE_FHC_ID = 'checkoutstep';

    public function hookToOrderSaveEvent()
    {
        if (Mage::helper('checkoutstep')->isEnabled()) {
            $order = new Mage_Sales_Model_Order ();
            $incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
            $order->loadByIncrementId($incrementId);

            // Fetch the data 
            $_checkoutstep_data = null;
            $_checkoutstep_data = Mage::getSingleton('core/session')->getIpragmatechCheckoutstep();
            $model = Mage::getModel('checkoutstep/customerdata')->setData(unserialize($_checkoutstep_data));
            $model->setData("order_id", $order["entity_id"]);
            try {
                $insertId = $model->save()->getId();
                Mage::log("Data successfully inserted. Insert ID: " . $insertId, null, 'mylog.log');
            } catch (Exception $e) {
                Mage::log("EXCEPTION " . $e->getMessage(), null, 'mylog.log');
            }
        }
    }
}

Magento – Add Custom Form in Checkout Extension is a complete solution to add extra step in Checkout process for your ecommerce website. It allow admin to export data from custom table in CSV format.

Visit the link to get this free extension.