I am absolutely new in magento. I have made new input called ‘name’ under email input in app/design/frontend/NewVendor/NewTheme/Magento_Newsletter/templates/subscribe.phtml
<div class="block newsletter">
<div class="title"><strong><?= $block->escapeHtml(__('Newsletter')) ?></strong></div>
<div class="content">
<form class="form subscribe"
novalidate
action="<?= $block->escapeUrl($block->getFormActionUrl()) ?>"
method="post"
data-mage-init='{"validation": {"errorClass": "mage-error"}}'
id="newsletter-validate-detail">
<div class="field newsletter">
<label class="label" for="newsletter"><span><?= $block->escapeHtml(__('Sign Up for Our Newsletter:')) ?></span></label>
<div class="control">
<input name="email" type="email" id="newsletter"
placeholder="<?= $block->escapeHtml(__('Enter your email address')) ?>"
data-mage-init='{"mage/trim-input":{}}'
data-validate="{required:true, 'validate-email':true}"/>
<input name="name" placeholder="Name"/>
</div>
</div>
<div class="actions">
<button class="action subscribe primary" title="<?= $block->escapeHtmlAttr(__('Subscribe')) ?>" type="submit">
<span><?= $block->escapeHtml(__('Subscribe')) ?></span>
</button>
</div>
</form>
</div>
I have made a new column in newsletter_subscriber table in app/code/Mag/Newsletter/Setup/UpgradeSchema.php
class UpgradeSchema implements UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '0.0.2', '<')) {
$setup->getConnection()->addColumn(
$setup->getTable('newsletter_subscriber'),
'name',
[
'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
'length' => 50,
'nullable' => false,
'default' => '',
'comment' => 'Name'
]
);
}
$setup->endSetup();
}
}
And this is my controller in app/code/Mag/Newsletter/Controller/Subscriber/NewAction.php
<?php
namespace MagebitNewsletterControllerSubscriber;
class NewAction extends
MagentoNewsletterControllerSubscriberNewAction
{
public function execute() {
$name = $this->getRequest()->getPost();
var_dump($name);exit;
}
}
For now controller var_dump’s input value.
What I want to achieve is to save input value into “name” column in ‘newsletter_subscriber’ table.
Can’t get it right.
What should I do next?
4
Answers
You have to get the instance of the Subscriber model and then use native method for example
and then
Please use below event to save your custom field data.
Step 1: Please create file events.xml in path Vendor/Module/etc/
Please use below event to save your custom field data.
Step 1: Please create file events.xml in path Vendor/Module/etc/
Step 2: Please create file newsletterSubscriberSave.php under path Vendor/Module/Observer/
_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;
}
}
First inject the MagentoNewsletterModelSubscriber class in your constructor:
Then you have two possible cases
Assuming your have the customer email
Then in your code you can call the following code to check whether or not the customer is subscribed to the newsletter:
Assuming you have the customer id
You can call the following code: