0
votes

I've been trying to add the subscriber first name to the Magento newsletter. I've can't find anything on Google. I've looked at Adding a custom field to Magento's subscription module but it's not clear for me. I have the field in newsletter_subscriber table called subscriber_firstname in the database and admin grid.

I created a module like this:

In app/etc/modules/Afe_Newslettername.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Afe_Newslettername>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Newsletter/>
            </depends>
        </Afe_Newslettername>
    </modules>
</config>

In app/code/local/Afe/Newslettername/etc/config.xml

<?xml version="1.0"?>
<config>    
<global>
<frontend>
    <events>
        <newsletter_subscriber_save_before>
            <observers>
                <afe_newslettername>
                    <class>afe_newslettername/observer</class>
                    <method>newsletterSubscriberSave</method>
                </afe_newslettername>
            </observers>
        </newsletter_subscriber_save_before>
    </events>
</frontend>
</global>
</config>

In app/code/local/Afe/Newslettername/Model/Observer.php

<?php
class Afe_Newslettername_Model_Observer
{
public function newsletterSubscriberSave(Varien_Event_Observer $observer)
{
    $subscriber = $observer->getEvent()->getSubscriber();
    $name = Mage::app()->getRequest()->getParam('subscriber_firstname');

    $subscriber->setSubscriberName($name);

    return $this;
}
}

...but the name doesn't gets saved to the database or displayed in the admin newsletter subscriber grid.

What am I missing?

UPDATE:

I changed my config.xml to:

<?xml version="1.0"?>
<config>
  <modules>
    <Afe_Newslettername>
      <version>1.0.0.0</version>
    </Afe_Newslettername>
  </modules>
  <global>
    <models>
      <afenewsletters>
        <class>Afe_Newslettername_Model</class>
      </afenewsletters>
    </models>
  </global>
       <events>
        <newsletter_subscriber_save_before>
            <observers>
                <add_name>
                    <class>afe_newslettername/observer</class>
                    <method>newsletterSubscriberSave</method>
                </add_name>
            </observers>
        </newsletter_subscriber_save_before>
       </events>
</config>

But now I get this error:

Warning: include(Mage/Afe/Newslettername/Model/Observer.php): failed to open stream: No such file or directory  in /home/www/public_html/lib/Varien/Autoload.php on line 94
Warning: include(): Failed opening 'Mage/Afe/Newslettername/Model/Observer.php' for inclusion (include_path='/home/www/public_html/app/code/local:/home/www/public_html/app/code/community:/home/www/public_html/app/code/core:/home/www/public_html/lib:.:/opt/cpanel/ea-php70/root/usr/share/pear')  in /home/www/public_html/lib/Varien/Autoload.php on line 94

Not sure why it's looking in the Mage folder?

1

1 Answers

0
votes

I think there are some issue in your event declaration code. Please try below code. Hope it works.

config.xml file:

<newsletter_subscriber_save_before>
    <observers>
        <afe_newslettername>
            <class>Afe_Newslettername_Model_Observer</class>
            <method>newsletterSubscriberSave</method>
        </afe_newslettername>
    </observers>
</newsletter_subscriber_save_before>

Observer.php file:

public function newsletterSubscriberSave($observer)