checkoutSession = $checkoutSession; $this->customerSession = $customerSession; $this->orderCustomerService = $orderCustomerService; parent::__construct($context); } /** * Execute request * * @throws AlreadyExistsException * @throws NoSuchEntityException * @throws \Exception * @return \Magento\Framework\Controller\Result\Json */ public function execute() { /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); if ($this->customerSession->isLoggedIn()) { return $resultJson->setData( [ 'errors' => true, 'message' => __('Customer is already registered') ] ); } $orderId = $this->checkoutSession->getLastOrderId(); if (!$orderId) { return $resultJson->setData( [ 'errors' => true, 'message' => __('Your session has expired') ] ); } try { $this->orderCustomerService->create($orderId); return $resultJson->setData( [ 'errors' => false, 'message' => __('A letter with further instructions will be sent to your email.') ] ); } catch (\Exception $e) { $this->messageManager->addExceptionMessage($e, $e->getMessage()); throw $e; } } }