How to extend community module in Magento 1.9

This is not gonna be a groundbreaking article. All the things in it are well known and can be found in dozens other places. However I do hope that you will find that last bit you are missing and get your extension/module working.

We are going to extend Vladimir Popov’s WebForms Pro 2 – a Magento extension for creating dynamic forms and storing the results in the store’s database. It is a great extension and there is nothing wrong with it. A client of ours wanted a little bit of additional functionality which does not exists. He wanted the ability to send a notification to different emails based on the country field selected by the customer. Generally the extension allows sending a notification only to one (administrative) account.

The following steps presume you are already somehow familiar with Magento’s code structure i.e. core/local/community folders.

  1. Create a new module in app/etc/modules/
    The module name we used is “HtmlPet_WebForms.xml” – HtmlPet is the name of our company and WebForms because we are going to extend the WebForms.

    The <depends> tag tells Magento that this mobule needs another one (VladimirPopov_WebForms) in order to work. That way we’ll ensure our extension will override the original.
  2. Create the minimal file structure required.There isn’t much to do with only the previous step so we need to create few folders.
    – app/code/local/HtmlPet/WebForms/etc
    – app/code/local/HtmlPet/WebForms/Block
    – app/code/local/HtmlPet/WebForms/Model
    – app/code/local/HtmlPet/WebForms/sqlWe are going to use the local code pool as we defined that in the module xml from step one. You can create a lot more folders than these but for the sake of our little extension these ones are enough.
  3. Creating a config.xml file.
    The first .xml file we created only tells Magento that there is a module. The config.xml is the one that “pulls the strings”. The basic one we need for now is:

    It will change in the next few steps.
  4. All so far are standard Magento stuff and they don’t do much. At this point it is good to know what we want to do.  Initially the WebForms interface looks like that.
    magento-webforms-original

    For the purpose of the task in question we need to add a new field to configure the different emails for the different countries. So we need to find which file adds all the original fields and then replace it in our extension. That file is app/code/community/VladimirPopov/WebForms/Block/Adminhtml/Edit/Tab/Email.php. Now when we know the file name will need to modify the config.xml file to tell Magento that we want to use different file. Since it is a block we need to add a <blocks> section in the <global> tag.

    The <adminhtml_webforms_edit_tab_email> tag tells magento we want to <rewrite> the file that matches it with the content of the tag.

    Then we need to copy the file app/code/community/VladimirPopov/WebForms/Block/Adminhtml/Edit/Tab/Email.php to app/code/local/HtmlPet/WebForms/Block/Adminhtml/Edit/Tab/Email.php

    Change the class name to reflect the new location i.e. “HtmlPet_WebForms_Block_Adminhtml_Webforms_Edit_Tab_Email”. In the newly created “local” file we can do pretty much everything without worrying that we will damage the webpage if the original extension updates their code. That’s the beauty of Magento.

    In this particular case we want to add a new textarea field and luckily the author has a pretty sweet way of adding fields and fieldsets so we only need to follow its lead.

    That code will transform the original form to this one.

    magento-webforms-modified

    So far, so good. Now we need to make that data field persistent.

  5. Add an SQL upgrade scriptIn order to save data to store’s database it has to be extended. That can happen with PhpMyAdmin and modifying the database but that solution is not flexible. If this module needs to be populated on another environment you have to modify the database in there too. So Magento give us pretty sweet way to extend the database (Read more about it http://inchoo.net/magento/magento-install-install-upgrade-data-and-data-upgrade-scripts/). Magento keeps track on your module’s version and if it detects that there is a change in the version it will search for an upgrade script. Or if there isn’t any track of your module being able to write to the database it will search for the install script. That is what we are going to create – an install script.First we need to tell Mageneto that our extension is going to write to the database.

    And after that create a file to execute. The file needs to be created at app/code/local/HtmlPet/WebForms/sql/webforms_setup_local/ and it has to have the name “install-0.1.0.php” where “webforms_setup_local” is the folder we entered in the <resource> file and 0.1.0 is the version of the module.

    Next time you refresh the admin area or the frontend Magento will create that field in the database.

  6. Use the configuration dataAt this point we have extended the original module (without touching it) so that it can save new field of data. For the purpose of the task we used the following format to enter the different emails for the different country.US:us_support@example.com
    GB:gb_support@example.co.uk
    BG:support@example.com

    First part is the country code and the second is the email that is going to handle that request. The last part is to redirect emails to the correct place. Original module sends emails via file called app/code/community/VladimirPopov/WebForms/Model/Results.php so that is the file we need to modify. Copy the file to your folder (app/code/local/HtmlPet/WebForms/Model) and change the name to reflect your structure i.e. HtmlPet_WebForms_Model_Results.

    In your new Results.php file find the function “sendEmail” and in it find this line:

    After it add

    Add this function to your Results.php

    This function parses the text we did input in the form configuration and returns the appropriate email. N.B. This extension assumes you are using a form with country field named “country”. If you are using a different name you need to adjust that in here:

    That is pretty much it. Looks like a lot but it is all logical (the Magento’s logic is solid, not my article ;)). If you have any questions or disagree with the way we did it, please let us know in the comments.

    You can find all the files in our Github repo – https://github.com/denchev/magento-extend-webforms