0

I created a plugin for Iways\PayPalPlus\Controller\Order\Create.php This is the function that I am observing.

 public function execute()
{
    try {
        $cartId = $this->checkoutSession->getQuoteId();
        $result = new DataObject();
        if ($this->customerSession->isLoggedIn()) {
            $orderId = $this->cartManagement->placeOrder($cartId);
        } else {
            $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'quote_id');
            $orderId = $this->guestCartManagement->placeOrder($quoteIdMask->getMaskedId());
        }

        if ($orderId) {
            $order = $this->orderFactory->create()->load($orderId);
            if ($order->getCanSendNewEmailFlag()) {
                try {
                    $this->orderSender->send($order);
                } catch (\Exception $e) {
                    $this->logger->critical($e);
                }
            }
            try {
                // IWD_Opc Order Comment
                if ($this->customerSession->getOrderComment()) {
                    if ($order->getData('entity_id')) {
                        /** @param string $status */
                        $status = $order->getData('status');
                        /** @param \Magento\Sales\Model\Order\Status\HistoryFactory $history */
                        $history = $this->historyFactory->create();
                        // set comment history data
                        $history->setData('comment', strip_tags($this->customerSession->getOrderComment()));
                        $history->setData('parent_id', $orderId);
                        $history->setData('is_visible_on_front', 1);
                        $history->setData('is_customer_notified', 0);
                        $history->setData('entity_name', 'order');
                        $history->setData('status', $status);
                        $history->save();
                        $this->customerSession->setOrderComment(null);
                    }
                }
            } catch (\Exception $e) {

            }

            $this->customerSession->setPayPalOrderId($orderId);
        }
        $result->setData('success', true);
        $result->setData('error', false);

        $this->_eventManager->dispatch(
            'checkout_controller_onepage_saveOrder',
            [
                'result' => $result,
                'action' => $this
            ]
        );
         $encryptedOrderId = $this->encryptor->encrypt($orderId);
         $this->_redirect('checkout/onepage/success',array('order'=>urlencode($encryptedOrderId)));
    } catch (\Exception $e) {
        $this->messageManager->addError($e->getMessage());
        $this->_redirect('checkout/cart');
    }

}

I want in my plugin, afterExecute method, to check weather orderId is set, so I can redirect to the appropriate url.

  • 1
    Is it okay if you try with event "checkout_controller_onepage_saveOrder" (it's being dispatched in your controller being observed) and check if order id is set, if yes than redirect to required url. You can redirect from observer as mentioned at magento.stackexchange.com/questions/112993/… – Himmat Paliwal Nov 28 '18 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.