Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Create.php
Go to the documentation of this file.
1 <?php
7 
11 
17 {
21  protected $checkoutSession;
22 
26  protected $customerSession;
27 
32 
40  public function __construct(
41  \Magento\Framework\App\Action\Context $context,
42  \Magento\Checkout\Model\Session $checkoutSession,
43  \Magento\Customer\Model\Session $customerSession,
44  \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService
45  ) {
46  $this->checkoutSession = $checkoutSession;
47  $this->customerSession = $customerSession;
48  $this->orderCustomerService = $orderCustomerService;
49  parent::__construct($context);
50  }
51 
60  public function execute()
61  {
63  $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
64 
65  if ($this->customerSession->isLoggedIn()) {
66  return $resultJson->setData(
67  [
68  'errors' => true,
69  'message' => __('Customer is already registered')
70  ]
71  );
72  }
73  $orderId = $this->checkoutSession->getLastOrderId();
74  if (!$orderId) {
75  return $resultJson->setData(
76  [
77  'errors' => true,
78  'message' => __('Your session has expired')
79  ]
80  );
81  }
82  try {
83  $this->orderCustomerService->create($orderId);
84  return $resultJson->setData(
85  [
86  'errors' => false,
87  'message' => __('A letter with further instructions will be sent to your email.')
88  ]
89  );
90  } catch (\Exception $e) {
91  $this->messageManager->addExceptionMessage($e, $e->getMessage());
92  throw $e;
93  }
94  }
95 }
__()
Definition: __.php:13
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Customer\Model\Session $customerSession, \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService)
Definition: Create.php:40