2

I want to redirect to new page after clicking to subscribe button. Can anyone help me over here.

  • After subscribing with a popup or just when the user puts the email in a textbox where they can subscribe? – Raül Jul 26 '18 at 11:57
1

You can redirect to a new page by creating an interceptor. Follow these steps:

  1. Create registration.php file at this location:

    app/code/Stack/NewsletterRedirect/
    
  2. Add this code to the registraton.php file:

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Stack_NewsletterRedirect',
        __DIR__
    );
    
  3. Create the module.xml at this location:

    app/code/Stack/NewsletterRedirect/etc
    
  4. Add these codes to the module.xml file:

    <?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>
    
  5. Now, you have to define the interceptor for Magento\Newsletter\Controller\Subscriber\NewAction at this location:

    app/code/Stack/NewsletterRedirect/etc/frontend/di.xml

  6. Add these lines to 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>
    
  7. Add these codes to Redirect.php file, which is located to app/code/Stack/NewsletterRedirect/Plugin/Subscriber/.

        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;
            }
    
        }
    
  8. Change contact/index/index to the URL of your custom page.

  9. Finally, run the following commands:

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

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.