0

I want to add an optional newsletter field in the checkout page (billing.phtml), costumer can insert his email to receive newsletter. The email must be insert in newsletter table.

I managed to create the field in billing.phtml But I can't find the way to insert data into : newsletters_suscriber/suscriber_email.

Anyone to help me ?

Thank you so much!

Here is my code :

 <label for="billing:custom_newsletter" class="required"><em>*</em><?php echo $this->__('Add your email for newsletters') ?></label>
    <div class="input-box">
     <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="billing[custom_newsletter]" id="billing:custom_newsletter" value="<?php echo $this->escapeHtml($this->getAddress()->getCustomNewsletters()) ?>" title="<?php echo $this->quoteEscape($this->__('newsletters')) ?>" class="input-text required-entry" />
</div>

And I added this code to Chekout/controllers/OnepageController.php

        if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
        $session            = Mage::getSingleton('core/session');
        $customerSession    = Mage::getSingleton('customer/session');
        $email              = (string) $this->getRequest()->getPost('billing_custom_newsletter');

        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);
            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.'));
        }

But it doesn't work

1 Answers1

0

you are trying to get the post value with key 'billing_custom_newsletter', but that does not exist because the name of the email field in the frontend is 'billing[custom_newsletter]'.

doing it like this creates an associative array in the global variable post. it will look something like this:

array(
...
'billing'=>array('custom_newsletter'=>{{value}},...)
...
)

you can either change the name of the field in the frontend to billing_custom_newsletter, or in the backend, first get the billing information from POST, then from there you can get your variable.

Example

$billing_info = $this->getRequest()->getPost('billing');
$custom_newsletter= $billing_info['custom_newsletter']
pauldrodriguez
  • 480
  • 2
  • 6