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