By default magento gives some checkout steps. But Sometime you need to fill extra information from the customer for future reference. A common requested customization is to add the Custom Form in default checkout process.
Custom Form in Checkout 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.
I assume that you already know the basic structure of Magento. Let’s try to implement the Custom redirection extension step by step.
Basic Structure
Step1: Create the Configuration file
<config> <modules> <Ipragmatech_Checkoutstep> <version>0.1.1</version> </Ipragmatech_Checkoutstep> </modules> <global> <models> <checkoutstep> <class>Ipragmatech_Checkoutstep_Model</class> <resourceModel>checkoutstep_resource</resourceModel> </checkoutstep> <checkoutstep_resource> <class>Ipragmatech_Checkoutstep_Model_Resource</class> <entities> <customerdata> <table>checkoutstep_customer_data</table> </customerdata> </entities> </checkoutstep_resource> <checkout> <rewrite> <type_onepage>Ipragmatech_Checkoutstep_Model_Type_Onepage</type_onepage> </rewrite> </checkout> </models> <helpers> <checkoutstep> <class>Ipragmatech_Checkoutstep_Helper</class> </checkoutstep> </helpers> <blocks> <checkout> <rewrite> <onepage>Ipragmatech_Checkoutstep_Block_Onepage</onepage> </rewrite> </checkout> <checkoutstep> <class>Ipragmatech_Checkoutstep_Block</class> </checkoutstep> <checkoutsteponepage> <class>Ipragmatech_Checkoutstep_Block_Onepage</class> </checkoutsteponepage> </blocks> <rewrite> <pragmaapps_checkoutstep_onepage> <from><![CDATA[#^/checkout/onepage/#]]></from> <to>/checkoutstep/onepage/</to> </pragmaapps_checkoutstep_onepage> </rewrite> <resources> <checkoutstep_setup> <setup> <module>Ipragmatech_Checkoutstep</module> </setup> </checkoutstep_setup> </resources> </global> <frontend> <routers> <pragmaapps_checkoutstep> <use>standard</use> <args> <module>Ipragmatech_Checkoutstep</module> <frontName>checkoutstep</frontName> </args> </pragmaapps_checkoutstep> </routers> <events> <checkout_onepage_controller_success_action> <observers> <hooksystem_order_success> <type>singleton</type> <class>checkoutstep/observer</class> <method>hookToOrderSaveEvent</method> </hooksystem_order_success> </observers> </checkout_onepage_controller_success_action> </events> <layout> <updates> <checkoutstep> <file>checkoutstep.xml</file> </checkoutstep> </updates> </layout> </frontend> <admin> <routers> <pragmaapps_checkoutstep> <use>admin</use> <args> <module>Ipragmatech_Checkoutstep</module> <frontName>checkoutstep</frontName> </args> </pragmaapps_checkoutstep> </routers> </admin> <adminhtml> <menu> <checkoutstep module="checkoutstep"> <title>Pragmaapps</title> <sort_order>71</sort_order> <children> <exportdata translate="title" module="checkoutstep"> <title>Export Data</title> <sort_order>1</sort_order> <action>checkoutstep/adminhtml_exportdata</action> </exportdata> </children> </checkoutstep> </menu> <acl> <resources> <admin> <children> <checkoutstep translate="title" module="checkoutstep"> <title>Pragmaapps</title> <sort_order>70</sort_order> <children> <exportdata translate="title"> <title>Export Data</title> <sort_order>10</sort_order> </exportdata> </children> </checkoutstep> </children> </admin> <admin> <children> <system> <children> <config> <children> <checkoutstep translate="title"> <title>Custom Checkout Step</title> </checkoutstep> </children> </config> </children> </system> </children> </admin> </resources> </acl> </adminhtml> </config>
In this configuration file the we have use the rewrite tag.Basically we want to add additional step in checkout process, for that we need to rewrite the checkout onepage method that why here rewrite tag is which overwrite the original onepage method to custom onepage checkout method.
Apart from that used checkout_onepage_controller_success_action hook event. This event is run when all checkout step is complete. I think no need to explain the other part of config file.
Step2: Create the System Configuration file
<config> <tabs> <Pragmaapps translate="label" module="checkoutstep"> <label>Pragmaapps Extension</label> <sort_order>300</sort_order> </Pragmaapps> </tabs> <sections> <checkoutstep translate="label"> <label>Checkout Setting</label> <tab>Pragmaapps</tab> <frontend_type>text</frontend_type> <sort_order>1001</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <groups> <settings translate="label"> <label>Custom Form In Checkout</label> <frontend_type>text</frontend_type> <sort_order>100</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <fields> <enabled translate="label comment"> <label>Checkout Setting</label> <frontend_type>select</frontend_type> <source_model>adminhtml/system_config_source_enabledisable</source_model> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </enabled> </fields> </settings> </groups> </checkoutstep> </sections> </config>
System Configuration file will create the fields in the backend. tag will create the Pragmaapps Tab in System Configuration of backend.
tag will create the checkout settting fields in backend. Here I have created the Module Enable/Disable setting field.
Step3: 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(); } }
Step4: Add steps which 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'); } //check that module is enable or not if (Mage::helper('checkoutstep')->isEnabled()) { $stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'checkoutstep', 'review'); } else { $stepCodes = array('billing', 'shipping', 'shipping_method', 'payment', 'review'); } foreach ($stepCodes as $step) { $steps[$step] = $this->getCheckout()->getStepData($step); } return $steps; } }
Step5: Grab the submitted value of custom form and set the values of Custom form
Open the pragmaapps > Checkoutstep > controllers > OnepageController.php and write the following fucntion.
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)); } }
Step6: 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' ); } } } }
Step6: Export data from custom table in CSV
Open the Ipragmatech > Checkoutstep > controllers > Adminhtml > ExportdataController.php and write the following code
class Ipragmatech_Checkoutstep_Adminhtml_ExportdataController extends Mage_Adminhtml_Controller_action { public function indexAction() { $this->loadLayout() ->_setActiveMenu('checkoutstep/adminhtml_exportdata') ->_addContent( $this->getLayout() ->createBlock('checkoutstep/adminhtml_exportdata') ->setTemplate('checkoutstep/exportdata.phtml')) ->renderLayout(); } public function postAction() { $post = $this->getRequest()->getPost(); try { if (empty($post)) { Mage::throwException($this->__('Invalid form data.')); } $read = Mage::getSingleton('core/resource')->getConnection('core_read'); $customerdata = $read->fetchAll("select * from checkoutstep_customer_data"); $columns = $read->fetchAll("SHOW COLUMNS FROM checkoutstep_customer_data "); $output = ""; //get the Table Header foreach ($columns as $column){ $output .='"'.$column['Field'].'",'; } $output .="n"; // Get Records from the table if (!empty($customerdata)) { foreach ($customerdata as $item){ $output .='"'.$item['id'].'",'; $output .='"'.$item['entrant_name'].'",'; $output .='"'.$item['entrant_phone'].'",'; $output .='"'.$item['entrant_email'].'",'; $output .='"'.$item['permanent_address'].'",'; $output .='"'.$item['address'].'",'; $output .='"'.$item['order_id'].'",'; $output .="n"; } } // Download the file $filename = "Customerdata.csv"; header('Content-type: application/csv'); header('Content-Disposition: attachment; filename='.$filename); echo $output; exit; $message = $this->__('Your form has been submitted successfully.'); Mage::getSingleton('adminhtml/session')->addSuccess($message); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } $this->_redirect('*/*'); } }
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.