Magento “Place Order” button redirection to custom Page before Order Succes Page
-
14-04-2021 - |
Question
Basically I want to display custom module page after "Checkout Page" and Before "Order Success Page".
I created the module manually and want to add a page between checkout page and order success page. On that page I have to collect some data from the users (data about the products that he/she selected), after place order button.
After collecting the data from the users, I have to redirect to the Order Success Page.
Any Solution would be much appreciated!
TIA!
Solution
Open your_company/your_module/etc/frontend/ create a file here event.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="checkout_onepage_controller_success_action">
<observer name="companyname_order_success" instance="CompanyName\ModuleName\Observer\Orderplaceafter" />
</event>
</config>
Then create an Observer folder in your module and place a file Orderplaceafter.php
<?php
namespace CompanyName\ModuleName\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
class Orderplaceafter implements ObserverInterface
{
protected $_responseFactory;
protected $_url;
public function __construct(
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\UrlInterface $url
) {
$this->_responseFactory = $responseFactory;
$this->_url = $url;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$event = $observer->getEvent();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_checkoutSession = $objectManager->create('\Magento\Checkout\Model\Session');
$_quoteFactory = $objectManager->create('\Magento\Quote\Model\QuoteFactory');
$order = $_checkoutSession->getLastRealOrder();
$quote = $_quoteFactory->create()->loadByIdWithoutStore($order->getQuoteId());
if ($quote->getId()) {
$quote->setIsActive(1)->setReservedOrderId(null)->save();
$_checkoutSession->replaceQuote($quote);
$url = $this->_url->getUrl('prescription/index');
$this->_responseFactory->create()->setRedirect($url)->sendResponse();
die();
}
}
}
Then run commands:
php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f php bin/magento cache:flush