0

I'm completely new to Magento and I was looking into ways I could implement a form that collects customer name and email information by writing the information into CSV file.

I know a custom module might be a way to solve this issues but I don't know how to implement it, or if you guys can think of a better way to solve this that would be much appreciated!

2
  • When do you want to collect that information? Commented Aug 13, 2018 at 17:25
  • I want to collect that information after the user clicks the submit button Commented Aug 13, 2018 at 17:26

1 Answer 1

0

You will have to create a custom module, as you have already noted. I can't give you the complete code, but here's an outline; if you try this and have a more specific question, just open a new one here.

app/etc/modules/Myname_Mymodule.xml This file tells Magento that your module is there.

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

app/code/local/Myname/Mymodule/etc/config.xml Your module configuration that tells Magento what your module does.

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Myname_Mymodule>
            <version>0.1.0</version>
        </Myname_Mymodule>
    </modules>
    <global>
        <models>
            <myname_mymodule>
                <class>Myname_Mymodule_Model</class>
            </myname_mymodule>
        </models>
        <helpers>
            <myname_mymodule>
                <class>Myname_Mymodule_Helper</class>
            </myname_mymodule>
        </helpers>
    </global>
    <frontend>
        <routers>
            <myname_mymodule>
                <use>standard</use>
                <args>
                    <module>Myname_Mymodule</module>
                    <frontName>myname_mymodule</frontName>
                </args>
            </myname_mymodule>
        </routers>
        <layout>
            <updates>
                <myname_mymodule>
                    <file>myname_mymodule.xml</file>
                </myname_mymodule>
            </updates>
        </layout>
    </frontend>
</config>

What the config.xml actually does, is: it registers the shortnames of your helpers, models and blocks that you can use in Mage::getModel(...), Mage::helper(...) and Mage::getLayout()->createBlock(...). It also registers a new URL (route). We will send your form there.

app/design/frontend/base/default/templates/myname_mymodule/form.phtml Put your form here :) Send it to the URL $this->getUrl('myname_mymodule/index/save')

app/code/local/Myname/Mymodule/controllers/IndexController.php

<?php

class Myname_Mymodule_IndexController extends Mage_Core_Controller_Front_Action {

    public function saveAction() {
        $postData = $this->getRequest()->getPost();

        // TODO validate post data
        // TODO save post data to CSV file


        // afterwards, show a nice message & redirect the user to the previous page
        $session = Mage::getSingleton('core/session');
        if($success) {
            $session->addSuccess(Mage::helper('myname_mymodule')->__("IT WORKED"));
        } else {
            $session->addError(Mage::helper('myname_mymodule')->__("Something went wrong."));
        }

        $this->_redirectReferer();
    }
}
2
  • Thank so much for all your help! I have created a new .phtml file for the form (/app/design/frontend/default/default/template/myname.phtml) and created a page where the block refers to this piece of code. For the submit button, I currently have it written as: <input type="submit" name="submit" value="<?php echo Mage::helper('contacts')->__('Submit') ?>" /> but should I make the submit button use the code to validate the post data and save post data to CSV file? I'm confused how this should all link together. Commented Aug 13, 2018 at 18:36
  • As @simonthesorcerer mentioned you have to put form action like this <form action="$this->getUrl('myname_mymodule/index/save')"> so after submit button click form action called saveAction of app/code/local/Myname/Mymodule/controllers/IndexController.php file where you can get information of form using this "$this->getRequest()->getPost()". Also for form validation you can refer this article inchoo.net/magento/out-of-the-box-form-validation-in-magento . Thanks.
    – Raj
    Commented Aug 14, 2018 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.