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
  • In which table you are try to save that value? And does those fields created into the table? Commented Aug 20, 2021 at 5:18
  • @DhirenVasoya field created in table but value showing null after susbscribe newsletter help me please
    – Mark
    Commented Aug 20, 2021 at 5:21
  • @DhirenVasoya magento.stackexchange.com/questions/344840/… please see my code and help me please
    – Mark
    Commented Aug 20, 2021 at 5:22
  • check the answer. I am sharing with you I wrote the module.magento.stackexchange.com/questions/344840/…
    – Devidas
    Commented Aug 20, 2021 at 6:49
  • @Devidas thank you so much bro its work for me
    – Mark
    Commented Aug 23, 2021 at 4:10

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
    Commented 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.