0

I'm trying to add first name and last name on my newsletter subscription. I would like to override the newsletter core functions of the controller and the subscriber model. Here is my code: config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
<modules> 
    <My_Newsletter> 
        <version>0.1.0</version> 
    </My_Newsletter> 
</modules> 
<global>
    <models>
        <newsletter>
            <rewrite>
                <subscriber>My_Newsletter_Model_Newsletter_Subscriber</subscriber>
            </rewrite>
        </newsletter>
    </models>
</global>
<frontend> 
    <routers> 
        <newsletter> 
            <args> 
                <modules> 
                    <My_Newsletter before="Mage_Newsletter_SubscriberController">My_Newsletter</My_Newsletter> 
                </modules> 
            </args> 
        </newsletter> 
    </routers> 
</frontend> 
</config>

Controller.php

<?php
include_once("Mage/Newsletter/controllers/SubscriberController.php");
class My_Newsletter_SubscriberController extends Mage_Newsletter_SubscriberController {
public function newAction()
{
    if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
        $session            = Mage::getSingleton('core/session');
        $customerSession    = Mage::getSingleton('customer/session');
        $email              = (string) $this->getRequest()->getPost('email');
        $firtname = (string) $this->getRequest()->getPost('firstname');
        $lastname = (string) $this->getRequest()->getPost('lastname');

        try {
            if (!Zend_Validate::is($email, 'EmailAddress')) {
                Mage::throwException($this->__('Please enter a valid email address.'));
            }

            if (Mage::getStoreConfig(Mage_Newsletter_Model_Subscriber::XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG) != 1 && 
                !$customerSession->isLoggedIn()) {
                Mage::throwException($this->__('Sorry, but administrator denied subscription for guests. Please <a href="%s">register</a>.', Mage::helper('customer')->getRegisterUrl()));
            }

            $ownerId = Mage::getModel('customer/customer')
                    ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
                    ->loadByEmail($email)
                    ->getId();
            if ($ownerId !== null && $ownerId != $customerSession->getId()) {
                Mage::throwException($this->__('This email address is already assigned to another user.'));
            }

            $status = Mage::getModel('newsletter/subscriber')->subscribe($email,$firtname,$lastname);
            if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
                $session->addSuccess($this->__('Confirmation request has been sent.'));
            }
            else {
                $session->addSuccess($this->__('Thank you for your subscription.'));
            }
        }
        catch (Mage_Core_Exception $e) {
            $session->addException($e, $this->__('There was a problem with the subscription: %s', $e->getMessage()));
        }
        catch (Exception $e) {
            $session->addException($e, $this->__('There was a problem with the subscription.'));
        }
    }
    $this->_redirectReferer();
}
}

subscriber.php

<?php
include_once("Mage/Newsletter/Model/Subscriber.php");
class My_Newsletter_Model_Subscriber extends Mage_Core_Model_Abstract {
const STATUS_SUBSCRIBED     = 1;
const STATUS_NOT_ACTIVE     = 2;
const STATUS_UNSUBSCRIBED   = 3;
const STATUS_UNCONFIRMED    = 4;

const XML_PATH_CONFIRM_EMAIL_TEMPLATE       = 'newsletter/subscription/confirm_email_template';
const XML_PATH_CONFIRM_EMAIL_IDENTITY       = 'newsletter/subscription/confirm_email_identity';
const XML_PATH_SUCCESS_EMAIL_TEMPLATE       = 'newsletter/subscription/success_email_template';
const XML_PATH_SUCCESS_EMAIL_IDENTITY       = 'newsletter/subscription/success_email_identity';
const XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE   = 'newsletter/subscription/un_email_template';
const XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY   = 'newsletter/subscription/un_email_identity';
const XML_PATH_CONFIRMATION_FLAG            = 'newsletter/subscription/confirm';
const XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG   = 'newsletter/subscription/allow_guest_subscribe';

/**
 * @deprecated since 1.4.0.1
 */
const XML_PATH_SENDING_SET_RETURN_PATH      = Mage_Core_Model_Email_Template::XML_PATH_SENDING_SET_RETURN_PATH;

/**
 * Prefix of model events names
 *
 * @var string
 */
protected $_eventPrefix = 'newsletter_subscriber';

/**
 * Parameter name in event
 *
 * In observe method you can use $observer->getEvent()->getObject() in this case
 *
 * @var string
 */
protected $_eventObject = 'subscriber';

/**
 * True if data changed
 *
 * @var bool
 */
protected $_isStatusChanged = false;

/**
 * Initialize resource model
 */
protected function _construct()
{
    $this->_init('newsletter/subscriber');
}

/**
 * Alias for getSubscriberId()
 *
 * @return int
 */
public function getId()
{
    return $this->getSubscriberId();
}

/**
 * Alias for setSubscriberId()
 *
 * @param int $value
 */
public function setId($value)
{
    return $this->setSubscriberId($value);
}

/**
 * Alias for getSubscriberConfirmCode()
 *
 * @return string
 */
public function getCode()
{
    return $this->getSubscriberConfirmCode();
}

/**
 * Return link for confirmation of subscription
 *
 * @return string
 */
public function getConfirmationLink() {
    return Mage::helper('newsletter')->getConfirmationUrl($this);
}

/**
 * Returns Insubscribe url
 *
 * @return string
 */
public function getUnsubscriptionLink() {
    return Mage::helper('newsletter')->getUnsubscribeUrl($this);
}

/**
 * Alias for setSubscriberConfirmCode()
 *
 * @param string $value
 */
public function setCode($value)
{
    return $this->setSubscriberConfirmCode($value);
}

/**
 * Alias for getSubscriberStatus()
 *
 * @return int
 */
public function getStatus()
{
    return $this->getSubscriberStatus();
}

/**
 * Alias for setSubscriberStatus()
 *
 * @param int
 */
public function setStatus($value)
{
    return $this->setSubscriberStatus($value);
}

/**
 * Set the error messages scope for subscription
 *
 * @param boolean $scope
 * @return Mage_Newsletter_Model_Subscriber
 */

public function setMessagesScope($scope)
{
    $this->getResource()->setMessagesScope($scope);
    return $this;
}

/**
 * Alias for getSubscriberEmail()
 *
 * @return string
 */
public function getEmail()
{
    return $this->getSubscriberEmail();
}

/**
 * Alias for setSubscriberEmail()
 *
 * @param string $value
 */
public function setEmail($value)
{
    return $this->setSubscriberEmail($value);
}

/**
 * Set for status change flag
 *
 * @param boolean $value
 */
public function setIsStatusChanged($value)
{
    $this->_isStatusChanged = (boolean) $value;
       return $this;
}

/**
 * Return status change flag value
 *
 * @return boolean
 */
public function getIsStatusChanged()
{
    return $this->_isStatusChanged;
}

/**
 * Return customer subscription status
 *
 * @return bool
 */
public function isSubscribed()
{
    if($this->getId() && $this->getStatus()==self::STATUS_SUBSCRIBED) {
        return true;
    }

    return false;
}


 /**
 * Load subscriber data from resource model by email
 *
 * @param int $subscriberId
 */
public function loadByEmail($subscriberEmail)
{
    $this->addData($this->getResource()->loadByEmail($subscriberEmail));
    return $this;
}

/**
 * Load subscriber info by customer
 *
 * @param Mage_Customer_Model_Customer $customer
 * @return Mage_Newsletter_Model_Subscriber
 */
public function loadByCustomer(Mage_Customer_Model_Customer $customer)
{
    $data = $this->getResource()->loadByCustomer($customer);
    $this->addData($data);
    if (!empty($data) && $customer->getId() && !$this->getCustomerId()) {
        $this->setCustomerId($customer->getId());
        $this->setSubscriberConfirmCode($this->randomSequence());
        if ($this->getStatus()==self::STATUS_NOT_ACTIVE) {
            $this->setStatus($customer->getIsSubscribed() ? self::STATUS_SUBSCRIBED : self::STATUS_UNSUBSCRIBED);
        }
        $this->save();
    }
    return $this;
}

/**
 * Returns sting of random chars
 *
 * @param int $length
 * @return string
 */
public function randomSequence($length=32)
{
    $id = '';
    $par = array();
    $char = array_merge(range('a','z'),range(0,9));
    $charLen = count($char)-1;
    for ($i=0;$i<$length;$i++){
        $disc = mt_rand(0, $charLen);
        $par[$i] = $char[$disc];
        $id = $id.$char[$disc];
    }
    return $id;
}

/**
 * Subscribes by email
 *
 * @param string $email
 * @throws Exception
 * @return int
 */
public function subscribe($email,$firstname,$lastname)
{
    $this->loadByEmail($email);
    $customerSession = Mage::getSingleton('customer/session');

    if(!$this->getId()) {
        $this->setSubscriberConfirmCode($this->randomSequence());
    }

    $isConfirmNeed   = (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1) ? true : false;
    $isOwnSubscribes = false;
    $ownerId = Mage::getModel('customer/customer')
        ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
        ->loadByEmail($email)
        ->getId();
    $isSubscribeOwnEmail = $customerSession->isLoggedIn() && $ownerId == $customerSession->getId();

    if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED
        || $this->getStatus() == self::STATUS_NOT_ACTIVE
    ) {
        if ($isConfirmNeed === true) {
            // if user subscribes own login email - confirmation is not needed
            $isOwnSubscribes = $isSubscribeOwnEmail;
            if ($isOwnSubscribes == true){
                $this->setStatus(self::STATUS_SUBSCRIBED);
            } else {
                $this->setStatus(self::STATUS_NOT_ACTIVE);
            }
        } else {
            $this->setStatus(self::STATUS_SUBSCRIBED);
        }
        $this->setSubscriberEmail($email);
        $this->setSubscriberFirstname($firstname);
        $this->setSubscriberLastname($lastname);
    }

    if ($isSubscribeOwnEmail) {
        $this->setStoreId($customerSession->getCustomer()->getStoreId());
        $this->setCustomerId($customerSession->getCustomerId());
    } else {
        $this->setStoreId(Mage::app()->getStore()->getId());
        $this->setCustomerId(0);
    }

    $this->setIsStatusChanged(true);

    try {
        $this->save();
        if ($isConfirmNeed === true
            && $isOwnSubscribes === false
        ) {
            $this->sendConfirmationRequestEmail();
        } else {
            $this->sendConfirmationSuccessEmail();
        }

        return $this->getStatus();
    } catch (Exception $e) {
        throw new Exception($e->getMessage());
    }
}

/**
 * Unsubscribes loaded subscription
 *
 */
public function unsubscribe()
{
    if ($this->hasCheckCode() && $this->getCode() != $this->getCheckCode()) {
        Mage::throwException(Mage::helper('newsletter')->__('Invalid subscription confirmation code.'));
    }

    $this->setSubscriberStatus(self::STATUS_UNSUBSCRIBED)
        ->save();
    $this->sendUnsubscriptionEmail();
    return $this;
}

/**
 * Saving customer subscription status
 *
 * @param   Mage_Customer_Model_Customer $customer
 * @return  Mage_Newsletter_Model_Subscriber
 */
public function subscribeCustomer($customer)
{
    $this->loadByCustomer($customer);

    if ($customer->getImportMode()) {
        $this->setImportMode(true);
    }

    if (!$customer->getIsSubscribed() && !$this->getId()) {
        // If subscription flag not set or customer is not a subscriber
        // and no subscribe below
        return $this;
    }

    if(!$this->getId()) {
        $this->setSubscriberConfirmCode($this->randomSequence());
    }

   /*
    * Logical mismatch between customer registration confirmation code and customer password confirmation
    */
   $confirmation = null;
   if ($customer->isConfirmationRequired() && ($customer->getConfirmation() != $customer->getPassword())) {
       $confirmation = $customer->getConfirmation();
   }

    $sendInformationEmail = false;
    if ($customer->hasIsSubscribed()) {
        $status = $customer->getIsSubscribed()
            ? (!is_null($confirmation) ? self::STATUS_UNCONFIRMED : self::STATUS_SUBSCRIBED)
            : self::STATUS_UNSUBSCRIBED;
        /**
         * If subscription status has been changed then send email to the customer
         */
        if ($status != self::STATUS_UNCONFIRMED && $status != $this->getStatus()) {
            $sendInformationEmail = true;
        }
    } elseif (($this->getStatus() == self::STATUS_UNCONFIRMED) && (is_null($confirmation))) {
        $status = self::STATUS_SUBSCRIBED;
        $sendInformationEmail = true;
    } else {
        $status = ($this->getStatus() == self::STATUS_NOT_ACTIVE ? self::STATUS_UNSUBSCRIBED : $this->getStatus());
    }

    if($status != $this->getStatus()) {
        $this->setIsStatusChanged(true);
    }

    $this->setStatus($status);

    if(!$this->getId()) {
        $storeId = $customer->getStoreId();
        if ($customer->getStoreId() == 0) {
            $storeId = Mage::app()->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
        }
        $this->setStoreId($storeId)
            ->setCustomerId($customer->getId())
            ->setEmail($customer->getEmail());
    } else {
        $this->setStoreId($customer->getStoreId())
            ->setEmail($customer->getEmail());
    }

    $this->save();
    $sendSubscription = $customer->getData('sendSubscription') || $sendInformationEmail;
    if (is_null($sendSubscription) xor $sendSubscription) {
        if ($this->getIsStatusChanged() && $status == self::STATUS_UNSUBSCRIBED) {
            $this->sendUnsubscriptionEmail();
        } elseif ($this->getIsStatusChanged() && $status == self::STATUS_SUBSCRIBED) {
            $this->sendConfirmationSuccessEmail();
        }
    }
    return $this;
}

/**
 * Confirms subscriber newsletter
 *
 * @param string $code
 * @return boolean
 */
public function confirm($code)
{
    if($this->getCode()==$code) {
        $this->setStatus(self::STATUS_SUBSCRIBED)
            ->setIsStatusChanged(true)
            ->save();
        return true;
    }

    return false;
}

/**
 * Mark receiving subscriber of queue newsletter
 *
 * @param  Mage_Newsletter_Model_Queue $queue
 * @return boolean
 */
public function received(Mage_Newsletter_Model_ $queue)
{
    $this->getResource()->received($this,$queue);
    return $this;
}

/**
 * Sends out confirmation email
 *
 * @return Mage_Newsletter_Model_Subscriber
 */
public function sendConfirmationRequestEmail()
{
    if ($this->getImportMode()) {
        return $this;
    }

    if(!Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE)
       || !Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY)
    )  {
        return $this;
    }

    $translate = Mage::getSingleton('core/translate');
    /* @var $translate Mage_Core_Model_Translate */
    $translate->setTranslateInline(false);

    $email = Mage::getModel('core/email_template');

    // $email->sendTransactional(
    //     Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE),
    //     Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY),
    //     $this->getEmail(),
    //     $this->getName(),
    //     array('subscriber'=>$this)
    // );

    $email->sendTransactional(
        Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
        Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
        $this->getEmail(),
        $this->getName(),
        array('subscriber'=>$this, 'unsubscribe' =>$this->getUnsubscriptionLink())
    );

    $translate->setTranslateInline(true);

    return $this;
}

/**
 * Sends out confirmation success email
 *
 * @return Mage_Newsletter_Model_Subscriber
 */
public function sendConfirmationSuccessEmail()
{
    if ($this->getImportMode()) {
        return $this;
    }

    if(!Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE)
       || !Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY)
    ) {
        return $this;
    }

    $translate = Mage::getSingleton('core/translate');
    /* @var $translate Mage_Core_Model_Translate */
    $translate->setTranslateInline(false);

    $email = Mage::getModel('core/email_template');

    $email->sendTransactional(
        Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
        Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
        $this->getEmail(),
        $this->getName(),
        array('subscriber'=>$this)
    );

    $translate->setTranslateInline(true);

    return $this;
}

/**
 * Sends out unsubsciption email
 *
 * @return Mage_Newsletter_Model_Subscriber
 */
public function sendUnsubscriptionEmail()
{
    if ($this->getImportMode()) {
        return $this;
    }
    if(!Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE)
       || !Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY)
    ) {
        return $this;
    }

    $translate = Mage::getSingleton('core/translate');
    /* @var $translate Mage_Core_Model_Translate */
    $translate->setTranslateInline(false);

    $email = Mage::getModel('core/email_template');

    $email->sendTransactional(
        Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_TEMPLATE),
        Mage::getStoreConfig(self::XML_PATH_UNSUBSCRIBE_EMAIL_IDENTITY),
        $this->getEmail(),
        $this->getName(),
        array('subscriber'=>$this)
    );

    $translate->setTranslateInline(true);

    return $this;
}

/**
 * Retrieve Subscribers Full Name if it was set
 *
 * @return string|null
 */
public function getSubscriberFullName()
{
    $name = null;
    if ($this->hasCustomerFirstname() || $this->hasCustomerLastname()) {
        $name = Mage::helper('customer')->getFullCustomerName($this);
    }
    return $name;
}
/**
* Alias for getSubscriberFirstname()
*
* @return string
*/
public function getFirstname()
{
    return $this->getSubscriberFirstname();
}

/**
* Alias for setSubscriberFirstName()
*
* @param string $value
*/
public function setFirstname($value)
{
    return $this->setSubscriberFirstname($value);
}

/**
* Alias for getSubscriberLastname()
*
* @return string
*/
public function getLastname()
{
    return $this->getSubscriberLastname();
}

/**
* Alias for setSubscriberLastname()
*
* @param string $value
*/
public function setLastname($value)
{
    return $this->setSubscriberLastname($value);
}
}

I add a getter and setter on the subscriber model add $firstname,$lastname on the subscribe function the problem is, when sending, the page doesn't load anymore and the admin list is not showing.

I suspect that I have a problem on the config.xml on overriding the subscriber.php model

0

Try to change suscriber.php like this :

<?php
class My_Newsletter_Model_Subscriber extends Mage_Newsletter_Model_Subscriber {
const STATUS_SUBSCRIBED     = 1;
const STATUS_NOT_ACTIVE     = 2;
const STATUS_UNSUBSCRIBED   = 3;
const STATUS_UNCONFIRMED    = 4;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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