2

Default behavior:

When I subscribed to newsletter then it displayed the success message in the same page itself.

What I want is:

When the customer or guest user subscribe to the newsletter I want them to be redirected to a custom page(cms page).In other words, I just want to display a separate thank you page after clicking "submit" button.

So, how can I achieve this?

Magento 2.2.6

  • You have to rewrite a controller then you change the redirect, or simply with an observer – PЯINCƏ Oct 15 '18 at 8:26
  • @PRINCE I don't know which controller file should be edited? So, can you tell me that? – Chintan Kaneriya Oct 15 '18 at 8:49
  • Controller/newsletter/subscriber/newAction.php – PЯINCƏ Oct 15 '18 at 9:15
2

Create a module with name STech_Subscription and create the files like below steps:

Step 1: Create registration.php under:

app/code/STech/Subscription/registration.php

with below content:

<?php

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

Step 2: Create module.xml under:

app/code/STech/Subscription/etc/module.xml

with below content:

Step 3: Create di.xml under:

app/code/STech/Subscription/etc/frontend/di.xml

with below content:

<?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="STech\Subscription\Controller\Subscriber\NewAction" />
</config>

Step 4: Create NewAction.php under:

app/code/STech/Subscription/Controller/Subscriber/NewAction.php

with below content:

<?php
namespace STech\Subscription\Controller\Subscriber;

use Magento\Customer\Api\AccountManagementInterface as CustomerAccountManagement;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\App\Action\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Newsletter\Model\SubscriberFactory;

class NewAction extends \Magento\Newsletter\Controller\Subscriber\NewAction
{

    protected $customerAccountManagement;
    protected $_urlInterface;

    public function __construct(
        Context $context,
        SubscriberFactory $subscriberFactory,
        Session $customerSession,
        StoreManagerInterface $storeManager,
        CustomerUrl $customerUrl,
        CustomerAccountManagement $customerAccountManagement,
        \Magento\Framework\UrlInterface $urlInterface
    ) {
        $this->_urlInterface = $urlInterface;
        parent::__construct($context, $subscriberFactory, $customerSession, $storeManager, $customerUrl, $customerAccountManagement);

    }


    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.'));
                }
            } 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->_urlInterface->getUrl('custom-thankyou')); // Edit tith your custom url
    }
}
2

If you don't need to change the subscribe functionality and only want to redirect to a different page, you can simply do this by writing a custom plugin (interceptor).

The steps are mentioned below:

Step 1: Create a file app/code/Stack/NewsletterRedirect/registration.php and write below code:

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

Step 2: Create the module.xml under app/code/Stack/NewsletterRedirect/etc folder and write below code in it:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Stack_NewsletterRedirect" setup_version="0.1.0"/>
</config>

Step 3: Define the plugin for Magento\Newsletter\Controller\Subscriber\NewAction in the app/code/Stack/NewsletterRedirect/etc/frontend/di.xml file:

<?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\Controller\Subscriber\NewAction">
        <plugin name="redirect_after_newslettter" type="Stack\NewsletterRedirect\Plugin\Subscriber\Redirect"/>
    </type>
</config>

Step 4: Write the following code in the plugin file (app/code/Stack/NewsletterRedirect/Plugin/Subscriber/Redirect.php):

<?php
namespace Stack\NewsletterRedirect\Plugin\Subscriber;
use Magento\Framework\App\Response\Http as responseHttp;
use Magento\Framework\UrlInterface;

class Redirect {

    public function __construct(
    responseHttp $response, UrlInterface $url
    ) {
        $this->response = $response;
        $this->_url = $url;
    }

    public function afterExecute(\Magento\Newsletter\Controller\Subscriber\NewAction $subject, $result) {
        $url = $this->_url->getUrl('contact/index/index');
        $this->response->setRedirect($url);
        return $result;
    }

}

Note: You can change contact/index/index to any other page URL.

Note: Don't forget to run the following commands after creating above files:

php bin/magento setup:upgrade
php bin/magento cache:clean

Please let me know if it works for you.

0

Sukumar's solution worked for me. In step 2 the code for the file module.xml is not showing, but you can see it here:

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
        <module name="STech_Subscription" setup_version="0.0.1">
            <sequence>
                <module name="Magento_Backend"/>
                <module name="Magento_Sales"/>
            </sequence>
        </module>
    </config>
  • With the redirect there is no need for the default newsletter success message. Any idea how to disable it? – Isaac Apr 23 at 18:54

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.