Create.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Controller\Account;
  7. use Magento\Framework\Controller\ResultFactory;
  8. use Magento\Framework\Exception\AlreadyExistsException;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. /**
  11. * @deprecated 100.2.5
  12. * @see DelegateCreate
  13. */
  14. class Create extends \Magento\Framework\App\Action\Action
  15. {
  16. /**
  17. * @var \Magento\Checkout\Model\Session
  18. */
  19. protected $checkoutSession;
  20. /**
  21. * @var \Magento\Customer\Model\Session
  22. */
  23. protected $customerSession;
  24. /**
  25. * @var \Magento\Sales\Api\OrderCustomerManagementInterface
  26. */
  27. protected $orderCustomerService;
  28. /**
  29. * @param \Magento\Framework\App\Action\Context $context
  30. * @param \Magento\Checkout\Model\Session $checkoutSession
  31. * @param \Magento\Customer\Model\Session $customerSession
  32. * @param \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService
  33. * @codeCoverageIgnore
  34. */
  35. public function __construct(
  36. \Magento\Framework\App\Action\Context $context,
  37. \Magento\Checkout\Model\Session $checkoutSession,
  38. \Magento\Customer\Model\Session $customerSession,
  39. \Magento\Sales\Api\OrderCustomerManagementInterface $orderCustomerService
  40. ) {
  41. $this->checkoutSession = $checkoutSession;
  42. $this->customerSession = $customerSession;
  43. $this->orderCustomerService = $orderCustomerService;
  44. parent::__construct($context);
  45. }
  46. /**
  47. * Execute request
  48. *
  49. * @throws AlreadyExistsException
  50. * @throws NoSuchEntityException
  51. * @throws \Exception
  52. * @return \Magento\Framework\Controller\Result\Json
  53. */
  54. public function execute()
  55. {
  56. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  57. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  58. if ($this->customerSession->isLoggedIn()) {
  59. return $resultJson->setData(
  60. [
  61. 'errors' => true,
  62. 'message' => __('Customer is already registered')
  63. ]
  64. );
  65. }
  66. $orderId = $this->checkoutSession->getLastOrderId();
  67. if (!$orderId) {
  68. return $resultJson->setData(
  69. [
  70. 'errors' => true,
  71. 'message' => __('Your session has expired')
  72. ]
  73. );
  74. }
  75. try {
  76. $this->orderCustomerService->create($orderId);
  77. return $resultJson->setData(
  78. [
  79. 'errors' => false,
  80. 'message' => __('A letter with further instructions will be sent to your email.')
  81. ]
  82. );
  83. } catch (\Exception $e) {
  84. $this->messageManager->addExceptionMessage($e, $e->getMessage());
  85. throw $e;
  86. }
  87. }
  88. }