Newsletter – Subscribe to Newsletter with a Checkbox in Custom Form

newsletter

I have created a custom form page where customers can submit details like name, email, etc and all the details are stored in a custom database table. The form includes following input fields.

Name Field:

<input id="name" name="name" value="<?php echo $_SESSION['name']; ?>" type="text" /> 

Email Field:

<input type="text" id="email" value="<?php echo $_SESSION['email']; ?>" name="email" />

In in the same form I have created another checkbox with subscription to newsletter option with the following code:

<?php
                        $email = $_POST['email'];
                        $status = Mage::getModel('newsletter/subscriber')->subscribe($email);
                        if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
                            Mage::getSingleton('core/session')
                               ->addSuccess($this->__('Confirmation request has been sent.'));
                        }
                        else {
                            Mage::getSingleton('core/session')
                                ->addSuccess($this->__('Thank you for your subscription.'));
                        }
                    ?>

In the Thank You page I do see the Thank you for your subscription. message and the subscription does happens in Magento but for some reason the email that the customers entered in the email field is not being posted to the newsletter so how do I fix it.

Update:

I was able to post the email to Magento Newsletter email field by moving the

    if (isset($_POST['is_subscribed'])) {
        $subscribeemail = $_POST['email'];
        $status = Mage::getModel('newsletter/subscriber')->subscribe($subscribeemail);
            if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
                Mage::getSingleton('core/session')
                 ->addSuccess($this->__('Confirmation request has been sent.'));
            }
            else {
                Mage::getSingleton('core/session')
                ->addSuccess($this->__('Thank you for your subscription.'));
            }
    }

inside QuoteController.php file of the module. Customer First Name is still empty so how do I post Name field as Customer First Name?

Best Answer

Get customer first name from customer/session and place it in the hidden field.

if (Mage::getSingleton('customer/session')->isLoggedIn()) {

    // Load the customer's data
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    $customer->getName(); // Full Name
    $customer->getFirstname();
}

or you can directly get First name in controller also.