0

Subscriber.php

<?php

namespace Vendor\Module\Plugin\Newsletter;

use Magento\Framework\App\Request\Http;

class Subscriber {
  protected $request;

  public function __construct(
    Http $request
  ) {
    $this->request = $request;
  }

  public function aroundSubscribe($subject, \Closure $proceed, $email) {
    if ($this->request->isPost() && $this->request->getPost('subscriber_name')) {
      $firstname = $this->getRequest->getPost('subscriber_name');
      print_r($firstname);
      $subject->setCFirstname($firstname);
      $result = $proceed($email);

      try {
        $subject->save();
      }catch (\Exception $e) {
        throw new \Exception($e->getMessage());
      }
    }

    return $result;
  }
}

subscribe.phtml

<div class="block newsletter">
  <div class="title"><strong><?php echo __('Newsletter') ?></strong></div>
  <div class="content">
    <form class="form subscribe"
    novalidate
    action="<?php echo $block->escapeUrl($block->getFormActionUrl()) ?>"
    method="post"
    data-mage-init='{"validation": {"errorClass": "mage-error"}}'
    id="newsletter-validate-detail">
    <div class="field firstname">
      <label class="label" for="firstname"><span><?php echo $block->escapeHtml(__('First Name')) ?></span></label>
      <div class="control">
        <input name="firstname" type="text" id="firstname"
        placeholder="<?php echo $block->escapeHtmlAttr(__('Subscriber Name')) ?>"
        data-validate="{required:true}"/>
      </div>
    </div>
    <div class="field newsletter">
      <label class="label" for="newsletter"><span><?php echo $block->escapeHtml(__('Sign Up for Our Newsletter:')) ?></span></label>
      <div class="control">
        <input name="email" type="email" id="newsletter"
        placeholder="<?php echo $block->escapeHtmlAttr(__('Enter your email address')) ?>"
        data-validate="{required:true, 'validate-email':true}"/>
      </div>
    </div>
    <div class="actions">
      <button class="action subscribe primary" title="<?php echo $block->escapeHtmlAttr(__('Subscribe')) ?>" type="submit">
        <span><?php echo $block->escapeHtml(__('Subscribe')) ?></span>
      </button>
    </div>
  </form>
</div>
</div>
0

Replace this line :

if ($this->request->isPost() && $this->request->getPost('subscriber_name'))
{

$firstname = $this->getRequest->getPost('subscriber_name');

with

if ($this->request->isPost() && $this->request->getPost('firstname'))
{

$firstname = $this->getRequest->getPost('firstname');

And then you need to save that like this :

$subject->setSubscriberName($firstname);
11
  • hey @Dhiren i am getting same issu after chage this Aug 19 at 14:00
  • my email field value is showing in database but name field value showing null Aug 19 at 14:01
  • is your databae field name is this one? setCFirstname Aug 19 at 14:02
  • database field name is Subscriber_name Aug 19 at 14:03
  • Then you need to save the value like this : $subject->setSubscriberName($firstname); Aug 19 at 14:06

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.