0

Is it possible to subscribe to a newsletter without the user having to create an account? So on a page have some fields, like email, name etc and store them in the newsletter data?

How would I do something like that?

My form code:

<? $pageid =  Mage::getBlockSingleton('cms/page')->getPage()->getId();

  if($pageid == '20'){
    echo '
    <form id="contact-form" action="https://natuurgroothandel.com/nieuwsbrief.php" method="post" class="contact_form row contact-form" role="form">
        <p class="col-sm-12 kl-fancy-form">
          <label class="control-label">Naam</label>
          <input type="text" name="name" class="form-control form-control-name" value="" tabindex="1" maxlength="35">
        </p>
        <p class="col-sm-12 kl-fancy-form">
          <label class="control-label">Geslacht</label>
          <input type="text" name="gender" class="form-control form-control-email h5-email" value="" tabindex="1" maxlength="35">
        </p>
        <p class="col-sm-12 kl-fancy-form">
          <label class="control-label">Geboortedatum</label>
          <input type="text" name="date" class="form-control form-control-phone" value="" tabindex="1" maxlength="35">
        </p>
        <p class="col-sm-12 kl-fancy-form">
          <label class="control-label">Emailadres</label>
          <input type="text" name="email" class="form-control form-control-phone" value="" tabindex="1" maxlength="35" required>
        </p>
        <div class="col-sm-12">
          <div class="error-container"></div>
        </div>
        <p class="col-sm-12">
          <button class="btn btn-fullcolor" type="submit">Verzenden</button>
        </p>
      </form>';
  }
?>
3
  • you need to take all required field from page and make it entry in all table that affect to this functionality. Commented Aug 23, 2017 at 10:05
  • @DhirenVasoya Can you explain a little more detailed?
    – twan
    Commented Aug 23, 2017 at 10:08
  • In default magento, when newsletter subscribe, at that time magento it self place entry in some table. You need to do same things here using custom code. Commented Aug 23, 2017 at 10:10

1 Answer 1

1

You can use this code to do this. Put this code in your controller action of form submission:

$email = $this->getRequest()->getPost('email_input_name');

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

        /* checking if customer is already subscribed or unsubscribed */
        if($subscriber->getStatus() != Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED &&
            $subscriber->getStatus() != Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED
        )
        {
            try
            {
                $subscriber->setImportMode(TRUE)->subscribe($email);
                // return or redirect to any page you want
            }
            catch (Exception $e)
            {
                Mage::logException($e);
            }
        }
6
  • Thank you, do you have an example of this working with a form?
    – twan
    Commented Aug 23, 2017 at 11:31
  • I used this code in an observer for order_place_after event.. you can have a simple html form with action="<?php echo Mage::getUrl('module/controller/action') ?>" and place this code in your action method. Commented Aug 24, 2017 at 6:10
  • Alright, where can I add the above code? I got a form only on the page I want and added the line in your above comment, but I don't see the maps: module/controller/action @Sachin Agarwal
    – twan
    Commented Aug 24, 2017 at 9:21
  • Do I need to create a module for this? I am really new to magento coding.
    – twan
    Commented Aug 24, 2017 at 9:25
  • @twan can you please add your form code in the question? Commented Aug 24, 2017 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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