How to redirect if custom attribute option value != 5431 to cms page else redirect to module Controller (prescription/index).
Till now both conditions redirecting to module Controller
<?php
namespace Softadroit\Prescription\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\App\ObjectManager;
use Psr\Log\LoggerInterface;
class Orderplaceafter implements ObserverInterface
{
protected $_responseFactory;
protected $_url;
protected $_order;
public function __construct(
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\UrlInterface $url,
\Magento\Sales\Api\Data\OrderInterface $order
) {
$this->_responseFactory = $responseFactory;
$this->_url = $url;
$this->_order = $order;
}
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');
$orderid = $observer->getEvent()->getOrderIds();
$order = $this->_order->load($orderid);
foreach($order->getItemsCollection() as $_item){
$product = $_item->getProductId();
//echo $_item->getName(); die();
$is_priscription = $_item->getProduct()->getMyCustomAttribute('prescription');
if($is_priscription != '5431'){
$customRedirectionUrl = $this->_url->getUrl('order-success');
return $this->_responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse();
}
}
$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'); //('[ModuleName]/[ModuleName]/[[Action]');
$this->_responseFactory->create()->setRedirect($url)->sendResponse();
die();
}
}
}