0

I want to save custom newsletter field in databse but only email value is showing in database after subscribe the newsletter .subscriber_name is showing null value This is my question How to save input value to database from custom newsletter magento2 Help me please

12

1 Answer 1

1

Please use below event to save your custom field data.

Step 1: Please create file events.xml in path Vendor/Module/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="newsletter_subscriber_save_before">
        <observer name="newsletter_subscriber_save_before" instance="Vendor\Module\Observer\newsletterSubscriberSave" />
    </event>
</config>

Step 2: Please create file newsletterSubscriberSave.php under path Vendor/Module/Observer/

<?php
namespace Vendor\Module\Observer;

use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;

class newsletterSubscriberSave implements ObserverInterface
{
    protected $_request;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request
    ) {
        $this->_request = $request;
    }

    public function execute(Observer $observer)
    {
        $subscriber = $observer->getEvent()->getSubscriber();
        $params = $this->_request->getParams();

        if(isset($params['subscriber_name'])) {
            $name = $params['subscriber_name'];
            $subscriber->setSubscriberName($name);
        }

        return $this;
    }
}

With the use of above code you can save custom field value. It will work for you.

Cheers!

1
  • 1
    Hey Thanks its work for me
    – Mark
    Aug 23, 2021 at 12:37

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.