1

I am currently implementing a behavior where I will check the quote items in cart, then check if it contains my custom attributes. If true, then it will redirect me to a specific page(CMS) page.
This behavior should trigger before place order is processed.

I already tried using, observer events: sales_order_place_before and checkout_submit_before. I was not successful in implementing the redirect in the observer. The place order process seems to ignore redirects from the observer.

Here is my code:

app/code/Package/ModuleName/etc/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_before">
    <observer name="observer_name" instance="Package\ModuleName\Observer\BeforePlaceOrder"/>
</event>

app/code/Package/ModuleMame/Observer/BeforePlaceOrder.php

 public function execute(Observer $observer)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    /** @var \Magento\Framework\UrlInterface $url */
    $url = $objectManager->create('\Magento\Framework\UrlInterface');

    /** @var \Magento\Framework\App\ResponseInterface $response */
    $response = $objectManager->create('\Magento\Framework\App\ResponseInterface');

    $customerBeforeAuthUrl = $url->getUrl('support-plan-option'); //you can change the routh path to your cms page
    $response->setRedirect($customerBeforeAuthUrl)->sendResponse();
}
5
  • 1
    My issue is related to this: magento.stackexchange.com/questions/165208/… I already tried their suggestions from the link. Oct 14, 2019 at 2:37
  • I think you are missing the return $response;
    – fmsthird
    Oct 14, 2019 at 6:51
  • I aldread tried returning the whole line of $response->setRedirect($customerBeforeAuthUrl)->sendResponse(); Oct 14, 2019 at 8:03
  • it seems the observer disregards your redirection code, what if you put die() or exit() after you return the $response. check if your redirect code will work after
    – fmsthird
    Oct 15, 2019 at 3:13
  • Hi @fnsthird, if I put die or exit, redirection won't go to my desired page it was redirected instead to checkout cart page due to the catch condition from native magento 2 place order process. Oct 15, 2019 at 11:03

2 Answers 2

0

Try using the following in your constructor:

  • \Magento\Framework\App\ResponseFactory to set the redirection and

  • \Magento\Framework\UrlInterface to set the URL to redirect.

6
  • I already tried this approach but still not working. Oct 14, 2019 at 3:05
  • how about using \Magento\Framework\App\Response\RedirectInterface ?
    – fmsthird
    Oct 14, 2019 at 3:13
  • also @EubieAluad can you confirm that your event observer is working?
    – fmsthird
    Oct 14, 2019 at 3:25
  • yes @fmsthird my observer is working . Also I have tried RedirectInterface. But still redirection isn't working Oct 14, 2019 at 4:19
  • can you include your code implementation in your post
    – fmsthird
    Oct 14, 2019 at 4:37
0

You can simply use this code in your observer to redirect on any specific page.

protected $_redirect;

protected $_actionFlag;

public function __construct(
    \Magento\Framework\App\Response\RedirectInterface $redirect,
    \Magento\Framework\App\ActionFlag $actionFlag
) {
    $this->_redirect = $redirect;
    $this->_actionFlag = $actionFlag;
}

public function execute(Observer $observer)
{
    $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
    $this->_redirect->redirect($controller->getResponse(), 'support-plan-option');
}

Hope this may help you. :)

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.