Registration.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Block;
  7. use Magento\Framework\View\Element\Template;
  8. /**
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class Registration extends \Magento\Framework\View\Element\Template
  13. {
  14. /**
  15. * @var \Magento\Checkout\Model\Session
  16. */
  17. protected $checkoutSession;
  18. /**
  19. * @var \Magento\Customer\Model\Session
  20. */
  21. protected $customerSession;
  22. /**
  23. * @var \Magento\Customer\Model\Registration
  24. */
  25. protected $registration;
  26. /**
  27. * @var \Magento\Customer\Api\AccountManagementInterface
  28. */
  29. protected $accountManagement;
  30. /**
  31. * @var \Magento\Sales\Api\OrderRepositoryInterface
  32. */
  33. protected $orderRepository;
  34. /**
  35. * @var \Magento\Sales\Model\Order\Address\Validator
  36. */
  37. protected $addressValidator;
  38. /**
  39. * @param Template\Context $context
  40. * @param \Magento\Checkout\Model\Session $checkoutSession
  41. * @param \Magento\Customer\Model\Session $customerSession
  42. * @param \Magento\Customer\Model\Registration $registration
  43. * @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
  44. * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
  45. * @param \Magento\Sales\Model\Order\Address\Validator $addressValidator
  46. * @param array $data
  47. * @codeCoverageIgnore
  48. */
  49. public function __construct(
  50. Template\Context $context,
  51. \Magento\Checkout\Model\Session $checkoutSession,
  52. \Magento\Customer\Model\Session $customerSession,
  53. \Magento\Customer\Model\Registration $registration,
  54. \Magento\Customer\Api\AccountManagementInterface $accountManagement,
  55. \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
  56. \Magento\Sales\Model\Order\Address\Validator $addressValidator,
  57. array $data = []
  58. ) {
  59. $this->checkoutSession = $checkoutSession;
  60. $this->customerSession = $customerSession;
  61. $this->registration = $registration;
  62. $this->accountManagement = $accountManagement;
  63. $this->orderRepository = $orderRepository;
  64. $this->addressValidator = $addressValidator;
  65. parent::__construct($context, $data);
  66. }
  67. /**
  68. * Retrieve current email address
  69. *
  70. * @return string
  71. * @codeCoverageIgnore
  72. */
  73. public function getEmailAddress()
  74. {
  75. return $this->checkoutSession->getLastRealOrder()->getCustomerEmail();
  76. }
  77. /**
  78. * Retrieve account creation url
  79. *
  80. * @return string
  81. * @codeCoverageIgnore
  82. */
  83. public function getCreateAccountUrl()
  84. {
  85. return $this->getUrl('checkout/account/delegateCreate');
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function toHtml()
  91. {
  92. if ($this->customerSession->isLoggedIn()
  93. || !$this->registration->isAllowed()
  94. || !$this->accountManagement->isEmailAvailable($this->getEmailAddress())
  95. || !$this->validateAddresses()
  96. ) {
  97. return '';
  98. }
  99. return parent::toHtml();
  100. }
  101. /**
  102. * Validate order addresses
  103. *
  104. * @return bool
  105. */
  106. protected function validateAddresses()
  107. {
  108. $order = $this->orderRepository->get($this->checkoutSession->getLastOrderId());
  109. $addresses = $order->getAddresses();
  110. foreach ($addresses as $address) {
  111. $result = $this->addressValidator->validateForCustomer($address);
  112. if (is_array($result) && !empty($result)) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. }