Blog

Magento 2 : How to add additional fields to newsletter

The newsletter subscription module in Magento has only one field (email) by default. If you want to add  an extra field to the form (like the name), perform the following steps:

    1. Register your Module
    2. Display new fields on the frontend
    3. Create New Fields in the table of Newsletter
    4. Save the DAta in the new fields in table
    5. Display that field in the admin panel

Let’s get started with the first step:-

Step 1: Register your Module

  1. Create file registration.php at app/code/Thecoachsmb/Newsletter/
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Thecoachsmb_Newsletter',
    __DIR__
);

2. Create module.xml file at app/code/Thecoachsmb/Newsletter/etc/

Step 2: Display new fields on the frontend

  1. Create your own layout with the same handle
    app/code/Thecoachsmb/Newsletter/view/frontend/layout/default.xml

Put there code with <referenceBlock> tag and original “name” of this block where you can redefine path to your custom template (be carefull with paths, they starts from view/frontend/templates in declared module) like

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="form.subscribe" template="Thecoachsmb_Newsletter::subscribe.phtml" />
    </body>
</page>

Then clear cache –

php bin/magento c:f

Extra one field got added in the newsletter as shown below:-

Step 3: Create New Fields in the table of Newsletter

1. Thecoachsmb/Newsletter/etc/db_schema.xml

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="newsletter_subscriber" resource="default" engine="innodb" comment="Newsletter Subscriber">
        <column xsi:type="varchar" name="subscriber_name" nullable="false" comment="Subscriber Name"/>
    </table>
</schema>
  1. Thecoachsmb/Newsletter/etc/di.xml
<?php

namespace Thecoachsmb\Newsletter\Plugin\Newsletter;

use Magento\Framework\App\Request\Http;
use Magento\Newsletter\Model\SubscriberFactory;
use Magento\Store\Model\StoreManagerInterface;

class SubscriptionManager
{
    protected $request;
   protected $subscriberFactory;
    protected $storeManager;

    public function __construct(Http $request, SubscriberFactory $subscriberFactory, StoreManagerInterface $storeManager)
    {
        $this->request = $request;
      $this->subscriberFactory = $subscriberFactory;
        $this->storeManager = $storeManager;
    }

    public function aroundSubscribe(\Magento\Newsletter\Model\SubscriptionManager $subject, callable $proceed, $email, $storeId)
    {
        if ($this->request->isPost() && $this->request->getPost('subscriber_name')) {

            $result = $proceed($email, $storeId);

            $subscriber_name = $this->request->getPost('subscriber_name');
            $websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
            $subscriber = $this->subscriberFactory->create()->loadBySubscriberEmail($email, $websiteId);

            if ($subscriber->getId()) {
                $subscriber->setSubscriberName($subscriber_name);
                $subscriber->save();
            }
        }
        return $result;
    }
}

3. Then clear cache –

php bin/magento c:f

Step 5: Display New Fields in the Admin Panel

To display new fields in the admin panel -> MARKETING -> Newsletter Subscribers

we need to declare it in xml file as shown below:
Create file – Thecoachsmb/Newsletter/view/adminhtml/layout/newsletter_subscriber_block.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="adminhtml.newslettrer.subscriber.grid.columnSet">
            <block class="Magento\Backend\Block\Widget\Grid\Column" name="adminhtml.newslettrer.subscriber.grid.columnSet.subscriber_name" as="subscriber_name">
                <arguments>
                    <argument name="header" xsi:type="string" translate="true">Name</argument>
                    <argument name="index" xsi:type="string">subscriber_name</argument>
                    <argument name="header_css_class" xsi:type="string">col-name</argument>
                    <argument name="column_css_class" xsi:type="string">col-name</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Then clear cache –

php bin/magento c:f
I hope you have learned below points :
1. Create Module
2. How to display new fields in the frontend
3. How to add new columns in the existing database
4. How to save data in the existing table
5. How to display new fields in the admin panel.
in this article.
Do comment below, mentioning the feedback. I would love to hear back from you !!

Leave a Reply

Your email address will not be published. Required fields are marked *

🚀 Let’s Connect on LinkedIn! 🚀

Want to level up your skills and knowledge? 🚀
Follow us on LinkedIn to get:
✅ Expert insights on business growth
✅ Daily tips to sharpen your skills
✅ Exclusive updates from The Coach SMB

Let's grow together!