1

How can we display a newsletter subscriber's email address in the frontend after they subscribe for a newsletter.

Currently, Magento 2 displays a message "Thank you for your subscription".

We need this changed to "Thank you for your subscription. A confirmation email will be sent to youremail@example.com within 10 minutes"

1
  • I think it's better to override or use Plugin Magento\Newsletter\Controller\Subscriber. Jan 18, 2020 at 4:28

2 Answers 2

0

Create a custom module as below for override the newsletter success message.

app/code/Vendor/Module/registration.php

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

app/code/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>

app/code/Vendor/Module/etc/frontend/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">
        <preference for="Magento\Newsletter\Controller\Subscriber\NewAction" type="Vendor\Module\Rewrite\Magento\Newsletter\Controller\Subscriber\NewAction"/>
</config>

app/code/Vendor/Module/Rewrite/Magento/Newsletter/Controller/Subscriber/NewAction.php

<?php
namespace Vendor\Module\Rewrite\Magento\Newsletter\Controller\Subscriber;

class NewAction extends \Magento\Newsletter\Controller\Subscriber\NewAction
{
    public function execute()
    {
        if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
            $email = (string)$this->getRequest()->getPost('email');

            try {
                $this->validateEmailFormat($email);
                $this->validateGuestSubscription();
                $this->validateEmailAvailable($email);

                $subscriber = $this->_subscriberFactory->create()->loadByEmail($email);
                if ($subscriber->getId()
                    && $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED
                ) {
                    throw new \Magento\Framework\Exception\LocalizedException(
                        __('This email address is already subscribed.')
                    );
                }

                $status = $this->_subscriberFactory->create()->subscribe($email);
                if ($status == \Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE) {
                    $this->messageManager->addSuccess(__('The confirmation request has been sent.'));
                } else {
                    $this->messageManager->addSuccess(__('Thank you for your subscription. A confirmation email will be sent to '.$email.' within 10 minutes'));
                }
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addException(
                    $e,
                    __('There was a problem with the subscription: %1', $e->getMessage())
                );
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong with the subscription.'));
            }
        }
        $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
    }

}

I have updated this line in the above file.

$this->messageManager->addSuccess(__('Thank you for your subscription. A confirmation email will be sent to '.$email.' within 10 minutes'));

Hope this will work for you.

2
0

I was trying to use Plugin. And avoiding overriding.

app/code/Vendor/Newsletter/etc/frontend/di.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Newsletter\Controller\Subscriber">
        <plugin name="customMessage" type="Vendor\Newsletter\Plugin\Controller\Subscriber" sortOrder="999"/>
    </type>
</config>

app/code/Vendor/Newsletter/Plugin/Controller/Subscriber.php

<?php declare(strict_types=1);

namespace Vendor\Newsletter\Plugin\Controller;

use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Message\MessageInterface;

class Subscriber
{
    /**
     * @var ManagerInterface
     */
    private $messageManager;

    /**
     * Subscriber constructor.
     * @param ManagerInterface $messageManager
     */
    public function __construct(
        ManagerInterface $messageManager
    ) {
        $this->messageManager = $messageManager;
    }

    /**
     * @param \Magento\Newsletter\Controller\Subscriber $subject
     * @param $result
     * @return mixed
     */
    public function afterExecute(
        \Magento\Newsletter\Controller\Subscriber $subject,
        $result
    ) {
        $this->addCustomMessages($subject);
        return $result;
    }

    private function addCustomMessages($subject)
    {
        $email = (string)$subject->getRequest()->getPost('email');
        $messages = $this->messageManager->getMessages();
        $count = $messages->getCountByType(MessageInterface::TYPE_SUCCESS);
        if ($count) {
            $messageItems = $messages->getItems();
            //$text = __('Thank you for your subscription.');
            foreach ($messageItems as $key => $message) {
                if ($message->getType() == MessageInterface::TYPE_SUCCESS) {
                    //Need to check more cases
                    $messages->deleteMessageByIdentifier($message->getIdentifier());
                    $this->messageManager->addSuccessMessage(
                        __('Thank you for your subscription. A confirmation email will be sent to %1 within 10 minutes', $email)
                    );
                }
            }
        }

    }
}

enter image description here

[Warning] Using this way can cause the losing success messages. We should check more cases to validate the correct message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.