Monday, September 9, 2013

Magento: Newsletter Custom fields Title, Firstname, lastname, email | How to add Title, Firstname, lastname, email in newsletter module.

Magento: Newsletter Custom fields Title, Firstname, lastname, email | How to add Title, Firstname, lastname, email in newsletter module.


We have provided solution to add custom input fields in magento newsletter module. How to add the custom input fields on newsletter section Title, Firstname, lastname, email. Also we implemented newsletter in magento page.

We need to follow the below steps,

Create newsletter form in html format

 
<form action="http://websitename,com/newsletter/subscriber/new/" method="post" id="newsletter-validate-detail">
<div class="form-subscribe"> <div class="input-box">
<table class="colors_lines_light"  border="0" cellspacing="1" cellpadding="5" align="center">
<tbody>
<tr >
<td width="95">Title:</td>
<td width="282"><select id="subtitle" name="subtitle" title="subtitle" class="required-entry">
                                    <option value="" selected="selected"></option>
                                    <option value="Mr">Mr</option>
                                    <option value="Ms">Ms</option>
                                    <option value="Mrs">Mrs</option>
                                    <option value="Sir">Sir</option>
                                    <option value="Lord">Lord</option>
                                    <option value="Dr">Dr</option>
                                    <option value="Prof">Prof</option>
                                    <option value="Rev">Rev</option>
                                </select>
</td>
</tr>
<tr >
<td width="95">First Name:</td>
<td width="282"><input type="text" name="subfirstname" id="subfirstname"  size="30" maxlength="50"  class="input-text required-entry" /></td>
</tr>
<tr >
<td width="95">Last Name:</td>
<td width="282"><input type="text" name="sublastname" id="sublastname"  size="30" maxlength="50" class="input-text required-entry" /></td>
</tr>
<tr >
<td width="95">Email Address</td>
<td width="282"> <input type="text" name="email" id="newsletter" title="Sign up for our newsletter" class="input-text required-entry validate-email" size="30" maxlength="100" ></td>
</tr>
<tr >
<td width="95">&nbsp;</td>
<td width="282"><button type="submit" title="Submit" class="button" ><span><span>Sign Up</span></span></button>
</td>
</tr>
</tbody>
</table>
  </div>  </div></form>

<script type="text/javascript">
//<![CDATA[
    var newsletterSubscriberFormDetail = new VarienForm('newsletter-validate-detail');
    new Varien.searchForm('newsletter-validate-detail', 'newsletter', '');
//]]>
</script>


 

Update grid.php in the following location

 
Location: app/code/core/Mage/Adminhtml/Block/Newsletter/Subscriber/


find the function  protected function _prepareColumns()    {    and add the below


 $this->addColumn('subtitle', array(
            'header'    => Mage::helper('newsletter')->__('Subscriber Title'),
            'index'     => 'subscriber_title',
            'default'   =>    '----'
        ));
       
        $this->addColumn('subfirstname', array(
            'header'    => Mage::helper('newsletter')->__('Subscriber First Name'),
            'index'     => 'subscriber_firstname',
            'default'   =>    '----'
        ));
        $this->addColumn('sublastname', array(
            'header'    => Mage::helper('newsletter')->__('Subscriber Last Name'),
            'index'     => 'subscriber_lastname',
            'default'   =>    '----'
        ));









 



update the SubscriberController.php in the below location

Location for update: app/code/core/Mage/Newsletter/controllers/


 find the function    public function newAction()    { 

 and  replace

  $status = Mage::getModel('newsletter/subscriber')->subscribe($email);  

add the below

 if ($this->getRequest()->getPost('subtitle'))
                {
                     $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
                     $subtitle     = (string) $this->getRequest()->getPost('subtitle');
                     $subscriber->setSubscriberTitle($subtitle);
                   
                }
               

                 if ($this->getRequest()->getPost('subfirstname'))
                {
              
                     $subfirstname     = (string) $this->getRequest()->getPost('subfirstname');
                     $subscriber->setSubscriberFirstname($subfirstname);
                   
                }
                if ($this->getRequest()->getPost('sublastname'))
                {
                   
                     $sublastname     = (string) $this->getRequest()->getPost('sublastname');
                     $subscriber->setSubscriberLastname($sublastname);
                   
                }
           
                    $status = Mage::getModel('newsletter/subscriber')->subscribe($email,$subtitle, $subfirstname, $sublastname);
           




 

update the Subscriber.php in the below locations

 
Location : app/code/core/Mage/Newsletter/Model/


 find function  

public function subscribe($email) 

and replace 

public function subscribe($email,$subtitle=null, $subfirstname=null, $sublastname=null)

in this function find 

$this->setSubscriberEmail($email);


add below following

            $this->setSubscriberTitle($subtitle);
            $this->setSubscriberFirstname($subfirstname);
            $this->setSubscriberLastname($sublastname);


 

Finally update the magento database,

  Now open the magento database and open the "newsletter_subscribers".
 Add the extra column what you want fields in the newsletter. Here we added three fields like subscriber_title, subscriber_firstname, subscriber_lastname











Hope this may be helpful. please share and provide suggestion to improve. thanks

By PHP with 5 comments

Friday, September 6, 2013

Magento: Newsletter Add first/last name customfields | How to add new fields in Magento newsletter

Magento: Newsletter Add first/last name customfields | How to add new fields in Magento newsletter.

>> First add two fields ( subscriber_firstname, subscriber_lastname) in newsletter_subscriber table and app\design\frontend\THEME\default\template\newsletter\subscribe.phtml.

    >> Now edit this page : app\code\core\Mage\Newsletter\controllers\SubscriberController.php

        Here you'll find "public function newAction()"
            Paste the below code after this following code:
            $status = Mage::getModel('newsletter/subscriber')->subscribe($email);    //line (63)

     ///////////////////////// First Name //////////////////////////////

            if ($this->getRequest()->getPost('subscriber_firstname'))
                {
                     $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
                     $name     = (string) $this->getRequest()->getPost('subscriber_firstname');
                     $subscriber->setSubscriberFirstname($name);
                     $subscriber->save();
                }

     //////////////////////  END  ///////////////////////////////////////

            Do same for field Last Name....

    >> The above code will effect your frontend. If you want to see the information from Admin too, you have to implement a little bit.

    Go to this link : app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber\grid.php
    Add this code :

    //It will show the information to the admin on News Letter Subscriber Section.
    $this->addColumn('subscriber_firstname', array(
            'header'    => Mage::helper('newsletter')->__('Subscriber First Name'),
            'index'     => 'subscriber_firstname',
            'default'   =>    '----'
        )); 

    *Note :- I would suggest to override the subscriber controller and admin blocks. In this post I've focused in the logic only and shown in 3 steps to understand you its very simple. But it's not good programming. You should follow the magento module structure. In my another post you'll see that how to override frontend and admin controllers.
   

By PHP with No comments

Advanced Newsletter Magento, How to add Custom field in Magento newsletter module,



Magento newsletter additional fields, Add fields in newsletter in magento, How to add new field(suscriber_name) in newsletter, How to add Custom field in Magento newsletter module, Advanced Newsletter Magento.

Some times you want to add custom field in newsletter module in magento. I hope this may be a basic brick for you to start. For now I’m going to explain here on adding country field to newsletter module in magento. You can follow this tutorial to add any custom fields in your newsletter module.
First step is to add new field called ‘country’ in “newsletter_subscriber” table inside your magento database. We’re going to save only country id’s in this field. So datatype can be VARCHAR(5 or 3). Because I don’t know how length is the country id. Mostly I’ve seen only two characters, but in case you know.
Now open the file,
1app\design\frontend\{YOUR_THEME}\default\template\newsletter\subscribe.phtml
Then add the following lines of code in your themes subscribe.phtml,
1<div class="input-box">
2  <?php echo Mage:getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()); ?>
3</div>
SUBSCRIBe_phtml
Now we’ve to get the country Id from the frontend select box using form POST method in controller. To do that we’ve to modify magento core files which is in
1app\code\core\Mage\Newsletter\controllers\SubscribeController.php
So as our ancestors says, please copy that file and place it in local folder for your store’s safety. I hope you know about these stuffs. Then open SubscribeController.php which is in local and edit the file to get the POST values from the form. Check out the function newAction() in that file. There’ll be a line declared with variable $email like given below:
1$email = (string) $this->getRequest()->getPost('email');
and enter this code below :
1$country = $this->getRequest()->getPost('country_id');
This is used to get the POST values from the front-end form in variable $country. Now we’ve to store it in database. So now find this line in the same function and modify it as follows:
1$status = Mage::getModel('newsletter/subscriber')->subscribe($email);
Just add the $country variable as a parameter to the function in that subscription function like this:
1$status = Mage::getModel('newsletter/subscriber')->subscribe($email,$country);
Check out the image below, if you don’t have any idea of what I’m saying:
controller
Now we’re gonna find out where that subscribe function is and then we’ll add the $country parameter to it. Yeah! now I can feel your frustation. Only 2 more steps baby, we’ll reach the destination. As you can see from this code
1Mage::getModel('newsletter/subscriber')
that  Subscriber.php file will be inside Newsletter folder within model folder like this:
1app\code\core\Mage\Newsletter\Model\subscriber.php
make a copy into local folder and edit the subscribe() function in that file.  Just add the $country variable as a parameter and add this line before the end of the function.
1$this->setCountry($country);
 This will set the country id in country field in “newsletter_subscriber” table. Check out the image below if you’ve any doubts in subscriber.php:subscribe_php
Now the form will be stored in db field country. If you dont believe check it in your DB dude! And finally one more step is there to show it in backend. Now open Grid.php from the following location:
1app\code\core\Mage\Adminhtml\Block\Newsletter\Subscriber
and copy it in local folder and edit the files to display country field in backend grid table.  There’ll be a function called _prepareColumns() in that Grid.php file.  Add these lines below $this->addColumn(‘email’, array(…)) :
1$this->addColumn('Country', array(
2            'header'    => Mage::helper('newsletter')->__('Country'),
3            'index'     => 'country',
4            'type'      => 'country'
5 ));
grid-table
That’s it! I hope everything works for you. Let me know if you have any doubts through comments.

By PHP with No comments

    • Popular
    • Categories
    • Archives