Question

I'm having a hard time with my custom module, I used the event customer_register_success.

Here's the part of my observer code. It will just give me an error 500. Just correct me if I'm wrong. Thanks

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

/**
 * This is the extension to redirect into thank you page of account creation
 *
 */
class Register implements ObserverInterface 
{
    protected $_responseFactory;
    protected $_redirect;
    protected $_url;

    public function __constrtuct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $redirect,
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_redirect = $redirect;
    }

     public function execute(\Magento\Framework\Event\Observer $observer)
     {
        $event = $observer->getEvent();
        $CustomRedirectionUrl = $this->_url->getUrl('thank-you-account-creation');
        $this->_redirect->setRedirect($CustomRedirectionUrl);

    }
}
Was it helpful?

Solution

Create events.xml at

app/code/Vendor/Module/etc/frontend/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="customer_register_success">
        <observer name="registerAfter" instance="Vendor\Module\Observer\RegisterAfter" />
    </event> 
</config>

Create RegisterAfter.php

app/code/Vendor/Module/Observer/RegisterAfter.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class RegisterAfter implements ObserverInterface
{
    protected $_responseFactory;

    protected $_redirect;

    protected $_url;

    public function __construct(
        \Magento\Framework\View\Element\BlockFactory $blockFactory,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $redirect
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_redirect = $redirect;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customRedirectionUrl = $this->_url->getUrl('thank-you-account-creation');
        $this->_responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse();
        die();           
    }
}

Note: Remove var/ or flush cache after adding observer

OTHER TIPS

you can use Magento\Framework\App\Response\Http\Interceptor for redirect from observer

public function __construct(
     ---------
    ResponseInterceptor $responseInterceptor
) {
    $this->responseInterceptor = $responseInterceptor;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    //your code
     $this->responseInterceptor->setRedirect($url)->sendResponse();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top