1

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);

    }
}

2 Answers 2

3

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

10
  • Hi @PrincePatel, Thanks I will try your code. I will comment back in here if it will work. Thanks Sep 19, 2017 at 9:35
  • Hi Prince Patel, I tried your code. and cleared var and cache. but it will just give me a 500 server error. I've been researching the whole day but no luck. Thanks Sep 19, 2017 at 9:45
  • @MazeStricks I tried your code and it's working perfectly in my system. Share your event xml. Sep 19, 2017 at 9:58
  • <?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="customer_register_success"> <observer name="custom_redirect_register_success" instance="MazeStricks\CreateAccount\Observer\Register"/> </event> </config> Sep 19, 2017 at 9:59
  • 1
    Hi, @Prince Patel!! Thank you so much, bro!!! your codes work great!!! I will accept this one as an answer to my question. your efforts are greatly appreciated. Thank you again Sep 19, 2017 at 10:08
1

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();
}
3
  • Hi @Yogesh Thanks for this one. I will try it also. I will give a feedback if this works. Thanks Sep 19, 2017 at 9:36
  • Hi, Yogesh, I tried your code. It's good because somehow it will not give me an error 500 but it will not redirect me to my desired page. Instead, it will just show a message in create account form. but when I check on the backend of the site the customer is added. Sep 19, 2017 at 9:57
  • try to change event from customer_register_success to customer_login Sep 19, 2017 at 10:06

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.