0

How can I save data in a new field in newsletter table of database? This is how I created my module based on other answers, but no one of aproaches worked. The fields of column c_firstname is always NULL. Magento version 2.4.3. This was what I have tried based on the following answers:

how i can save the data from custom newsletter to database

Magento 2 : save additional attributes in newsletter

Magento 2 : How to add additional fields to newsletter

Please, someone could help me?

Thanks in advance!

Vendor/Module/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Newsletter\Model\Subscriber">
    <plugin name="vendor_newsletters_newsletter_model_subscriber" type="Vendor\Module\Plugin\Model\Subscriber" sortOrder="1" />
  </type>
</config>

Vendor/Module/etc/events.xml

<?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>

Vendor/Module/etc/module.xml

<?xml version="1.0" ?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Newsletter" />
        </sequence>
    </module>
</config>

Vendor/Observer/newsletterSubscriberSave.php

<?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['c_firstname'])) {
            $name = $params['c_firstname'];
            $subscriber->setSubscriberName($name);
        }

        return $this;
    }
}

Vendor/Module/Plugin/Model/Subscriber.php

<?php

namespace Vendor\Module\Plugin\Model;

use Magento\Framework\App\Request\Http;

class Subscriber {
    protected $request;
    
    public function __construct(Http $request){
        $this->request = $request;
    }

    public function aroundSubscribe(\Magento\Newsletter\Model\Subscriber $subject, \Closure $proceed, $email) {

        if ($this->request->isPost() && $this->request->getPost('c_firstname')) { 
            
            $firstname = $this->request->getPost('c_firstname');
            /* $lastname = $this->request->getPost('lastname'); */

            $subject->setCFirstname($firstname);
            /* $subject->setCLastname($lastname); */
            $result = $proceed($email);
            
            try {
                $subject->save();
            }catch (\Exception $e) {
                throw new \Exception($e->getMessage());
            }
        }

        return $result;
    }
}

Vendor/Module/etc/db_schema.xml

<?xml version="1.0"?>    
<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="c_firstname" nullable="false" comment="First Name"/>
        <column xsi:type="varchar" name="c_lastname" nullable="false" comment="Last Name"/>
    </table>
</schema>

Vendor/Module/view/adminhtml/layout/newsletter_subscriber_block.xml

<?xml version="1.0"?>    
<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.newsletter.subscriber.container">
            <block class="Magento\Newsletter\Block\Adminhtml\Subscriber\Grid" name="adminhtml.newslettrer.subscriber.grid" as="grid">
                <arguments>
                    <argument name="id" xsi:type="string">subscriberGrid</argument>
                    <argument name="dataSource" xsi:type="object">Magento\Newsletter\Model\ResourceModel\Subscriber\Grid\Collection</argument>
                    <argument name="default_sort" xsi:type="string">subscriber_id</argument>
                    <argument name="default_dir" xsi:type="string">desc</argument>
                    <argument name="use_ajax" xsi:type="string">1</argument>
                </arguments>
                <block class="Magento\Backend\Block\Widget\Grid\Massaction" name="adminhtml.newslettrer.subscriber.grid.massaction" as="grid.massaction">
                    <arguments>
                        <argument name="massaction_id_field" xsi:type="string">subscriber_id</argument>
                        <argument name="form_field_name" xsi:type="string">subscriber</argument>
                        <argument name="use_select_all" xsi:type="string">1</argument>
                        <argument name="options" xsi:type="array">
                            <item name="unsubscribe" xsi:type="array">
                                <item name="label" xsi:type="string" translate="true">Unsubscribe</item>
                                <item name="url" xsi:type="string">*/*/massUnsubscribe</item>
                            </item>
                            <item name="delete" xsi:type="array">
                                <item name="label" xsi:type="string" translate="true">Delete</item>
                                <item name="url" xsi:type="string">*/*/massDelete</item>
                            </item>
                        </argument>
                    </arguments>
                </block>
                <block class="Magento\Backend\Block\Widget\Grid\Export" name="adminhtml.newslettrer.subscriber.grid.export" as="grid.export">
                    <arguments>
                        <argument name="exportTypes" xsi:type="array">
                            <item name="csv" xsi:type="array">
                                <item name="urlPath" xsi:type="string">*/*/exportCsv</item>
                                <item name="label" xsi:type="string" translate="true">CSV</item>
                            </item>
                            <item name="excel" xsi:type="array">
                                <item name="urlPath" xsi:type="string">*/*/exportXml</item>
                                <item name="label" xsi:type="string" translate="true">Excel XML</item>
                            </item>
                        </argument>
                    </arguments>
                </block>
                <block class="Magento\Backend\Block\Widget\Grid\ColumnSet" name="adminhtml.newslettrer.subscriber.grid.columnSet" as="grid.columnSet">
                    <arguments>
                        <argument name="id" xsi:type="string">problemGrid</argument>
                    </arguments>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="subscriber_id">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">ID</argument>
                            <argument name="index" xsi:type="string">subscriber_id</argument>
                            <argument name="header_css_class" xsi:type="string">col-id</argument>
                            <argument name="column_css_class" xsi:type="string">col-id</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="email">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Email</argument>
                            <argument name="index" xsi:type="string">subscriber_email</argument>
                            <argument name="header_css_class" xsi:type="string">col-email</argument>
                            <argument name="column_css_class" xsi:type="string">ccol-email</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="type">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Type</argument>
                            <argument name="index" xsi:type="string">type</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="array">
                                <item name="guest" xsi:type="array">
                                    <item name="value" xsi:type="string">1</item>
                                    <item name="label" xsi:type="string" translate="true">Guest</item>
                                </item>
                                <item name="customer" xsi:type="array">
                                    <item name="value" xsi:type="string">2</item>
                                    <item name="label" xsi:type="string" translate="true">Customer</item>
                                </item>
                            </argument>
                            <argument name="header_css_class" xsi:type="string">col-type</argument>
                            <argument name="column_css_class" xsi:type="string">col-type</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="firstname">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Customer First Name</argument>
                            <argument name="index" xsi:type="string">firstname</argument>
                            <argument name="renderer" xsi:type="string">Vendor\Module\Block\Newsletter\Adminhtml\Template\Grid\Renderer\Customerfirstname</argument>
                            <argument name="default" xsi:type="string">----</argument>
                            <argument name="header_css_class" xsi:type="string">col-first-name</argument>
                            <argument name="column_css_class" xsi:type="string">col-first-name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="lastname">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Customer Last Name</argument>
                            <argument name="index" xsi:type="string">lastname</argument>
                            <argument name="renderer" xsi:type="string">Vendor\Module\Block\Newsletter\Adminhtml\Template\Grid\Renderer\Customerlastname</argument>
                            <argument name="default" xsi:type="string">----</argument>
                            <argument name="header_css_class" xsi:type="string">col-last-name</argument>
                            <argument name="column_css_class" xsi:type="string">col-last-name</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="status">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Status</argument>
                            <argument name="index" xsi:type="string">subscriber_status</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="array">
                                <item name="status_not_active" xsi:type="array">
                                    <item name="value" xsi:type="string">2</item>
                                    <item name="label" xsi:type="string" translate="true">Not Activated</item>
                                </item>
                                <item name="status_subscribed" xsi:type="array">
                                    <item name="value" xsi:type="string">1</item>
                                    <item name="label" xsi:type="string" translate="true">Subscribed</item>
                                </item>
                                <item name="status_unsubscribed" xsi:type="array">
                                    <item name="value" xsi:type="string">3</item>
                                    <item name="label" xsi:type="string" translate="true">Unsubscribed</item>
                                </item>
                                <item name="status_unconfirmed" xsi:type="array">
                                    <item name="value" xsi:type="string">4</item>
                                    <item name="label" xsi:type="string" translate="true">Unconfirmed</item>
                                </item>
                            </argument>
                            <argument name="header_css_class" xsi:type="string">col-status</argument>
                            <argument name="column_css_class" xsi:type="string">col-status</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column\Multistore" as="website">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Web Site</argument>
                            <argument name="index" xsi:type="string">website_id</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Magento\Config\Model\Config\Source\Website\OptionHash"/>
                            <argument name="header_css_class" xsi:type="string">col-website</argument>
                            <argument name="column_css_class" xsi:type="string">col-website</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column\Multistore" as="group">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Store</argument>
                            <argument name="index" xsi:type="string">group_id</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Magento\Newsletter\Block\Subscribe\Grid\Options\GroupOptionHash"/>
                            <argument name="header_css_class" xsi:type="string">col-store</argument>
                            <argument name="column_css_class" xsi:type="string">col-store</argument>
                        </arguments>
                    </block>
                    <block class="Magento\Backend\Block\Widget\Grid\Column\Multistore" as="store">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Store View</argument>
                            <argument name="index" xsi:type="string">store_id</argument>
                            <argument name="type" xsi:type="string">options</argument>
                            <argument name="options" xsi:type="options" model="Magento\Newsletter\Block\Subscribe\Grid\Options\StoreOptionHash"/>
                            <argument name="header_css_class" xsi:type="string">col-store-view</argument>
                            <argument name="column_css_class" xsi:type="string">col-store-view</argument>
                        </arguments>
                    </block>
                </block>
            </block>
        </referenceBlock>
    </body>
</page>

Vendor/Module/view/frontend/layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="head.components">
            <block class="Magento\Framework\View\Element\Js\Components" name="newsletter_head_components" template="Magento_Newsletter::js/components.phtml"/>
        </referenceBlock>
        
        <referenceContainer name="page.bottom.container">
            <block class="Magento\Newsletter\Block\Subscribe" name="form.subscribe.new" before="-" template="Vendor_Module::subscribe.phtml"/>
        </referenceContainer>
        <referenceBlock name="form.subscribe" remove="true" />
    </body>
</page>

Vendor/Module/view/frontend/templates/subscribe.phtml

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var \Magento\Newsletter\Block\Subscribe $block */

?>
<div class="newsletter">
    <div class="content">
        <form class="form subscribe container" novalidate
              action="<?php echo $block->escapeUrl($block->getFormActionUrl()) ?>"
              method="post"
              data-mage-init='{"validation": {"errorClass": "mage-error"}}'
              id="newsletter-validate-detail">
              <div class="right">
                <div class="inputs">
                    <div class="field firstname">                
                        <div class="control">
                            <input name="c_firstname" type="text" id="c_firstname" placeholder="<?php echo $block->escapeHtml(__('Type your first name')) ?>" data-validate="{required:true}"/>
                        </div>
                    </div>
                    <!-- <div class="field lastname">
                        <label class="label" for="lastname"><span><?php echo $block->escapeHtml(__('Last Name')) ?></span></label>
                        <div class="control">
                            <input name="lastname" type="text" id="lastname" placeholder="<?php echo $block->escapeHtml(__('Type your last name')) ?>" data-validate="{required:true}"/>
                        </div>
                    </div> -->
                    <div class="field newsletters">                
                        <div class="control">
                            <input name="email" type="email" id="newsletter" placeholder="<?php echo $block->escapeHtml(__('Type you email')) ?>" data-validate="{required:true, 'validate-email':true}"/>
                        </div>
                    </div>
                    <div class="actions">
                        <button class="action subscribe primary button -black" title="<?php echo $block->escapeHtml(__('Win your discount!')) ?>" type="submit">
                            <span><?php echo $block->escapeHtml(__('Win your discount!')) ?></span>
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

Vendor/Module/registration.php

<?php

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

1 Answer 1

0

In your example you are using outdated logic

In Magento 2.4.3 uses another way to subscribe. See \Magento\Newsletter\Model\SubscriptionManagerInterface.

You can implement you own logic for this interface and define preference

<preference for="Magento\Newsletter\Model\SubscriptionManagerInterface"
    type="Acme\StackExchange\Model\SubscriptionManager"/>

or create plugin for \Magento\Newsletter\Model\SubscriptionManager

Also you can see subscribe logic in \Magento\Newsletter\Controller\Subscriber\NewAction::execute

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.