How to add a custom field to Magento subscription form

By default, Magento newsletter module provides us with the only one field – email. In this article, I will show you how to add a custom field to the subscription form. In our case, this will be Country dropdown.

The main steps for adding our new field are:

  1. Add new column to the table newsletter_subscriber
  2. Add dropdown to the subscription form
  3. Create an observer method for newsletter_subscriber_save_before event

Let’s create a separate extension Snowcore_Subscriber.

 

Adding new table column

This can be done using simple installer script:

Changes in the template

This example is based on the Magento Community version 1.9.2.2, so the template file for the default responsive theme is located by the following path: app/design/frontend/rwd/default/template/newsletter/subscribe.phtml (feel free to copy this file to your custom theme). There is already one line needs to be added here:

So, the full template code will look like this:

I have not used any styling, so it looks not perfect. Feel free to add some styling according to your custom Magento theme.

 

Saving the data

We define observer method in the config.xml file:

Here is the observer method itself:

This allows saving the country code value to the newsletter subscriber table. From now, you can use this field anywhere you need. For example, in the newsletter template this value can be referenced like this:

 

Optional steps

The main functionality works fine, but it will be useful to see our country column on the subscribers grid in the admin part. I suggest using another observer method for event core_block_abstract_prepare_layout_before

Here is the method that adds custom column to the grid:

I have created the module and published it on the GitHub, so feel free to check it here https://github.com/snowcore/newsltetter_subscriber

 

  • Magenter

    Nice and quick 🙂

  • TheKitMurkit

    It looks like a bad idea to modify default subscription template. There should probably be a way to add a custom template through the module?

    • Snowcore

      There is a suggestion to override the template: “feel free to copy this file to your custom theme”

  • Shaun

    Thanks for the article.

    If I want to save back a hidden text field which is just recording the source location of the subscriber how would I go about saving this back to the database?

    I’ve managed to add a new column to the newsletter_subscriber table named ‘subscriber_source’ and this is showing successfully in the Magento newsletter subscriber grid if I manually edit a value in the table. I’m just having trouble saving my input field to the database from a post.

    The subscribe.phtml file contains the below in case this is the issue…

    Many thanks.