0

There is one link on my product page. If the customer is not login than its ask for customer login in the popup and after login, I want to redirect that product page.

So, I am using customer_login event and in popup login form I am passing product id and save some detail to the database. Now I want to redirect that product page.

in Magento 1 redirect from observer like this

$observer->getRequest()->setParam('return_url','http://www.google.com/');

so how can I do this from an observer in Magento 2?

2 Answers 2

0

Use below code

protected $resultRedirect;

public function __construct(Context $context,ResultFactory $result) {       
        $this->resultRedirect = $result;        
        parent::__construct($context);  
    }

Add below code to your function

$resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl('add-url-here');
return $resultRedirect;
2
  • Not working...Its redirect to Customer Account page
    – Newbie
    Commented Apr 7, 2016 at 11:45
  • Post your code in question Commented Apr 7, 2016 at 12:15
0

This code is works

public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager
) {
    $this->_objectManager = $objectManager;
 }

Use setBeforeAuthUrl redirect after login

$this->_objectManager->get('Magento\Customer\Model\Session')->setBeforeAuthUrl($productCollection->getProductUrl());

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.