Skip to content

Commit 21db8d6

Browse files
committed
MAGETWO-40134: Exception when creating account for guest customer after placing order within Express Checkout
1 parent 387941b commit 21db8d6

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

app/code/Magento/Checkout/Block/Registration.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,24 @@ class Registration extends \Magento\Framework\View\Element\Template
2929
*/
3030
protected $accountManagement;
3131

32+
/**
33+
* @var \Magento\Sales\Api\OrderRepositoryInterface
34+
*/
35+
protected $orderRepository;
36+
37+
/**
38+
* @var \Magento\Sales\Model\Order\Address\Validator
39+
*/
40+
protected $addressValidator;
41+
3242
/**
3343
* @param Template\Context $context
3444
* @param \Magento\Checkout\Model\Session $checkoutSession
3545
* @param \Magento\Customer\Model\Session $customerSession
3646
* @param \Magento\Customer\Model\Registration $registration
3747
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
48+
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
49+
* @param \Magento\Sales\Model\Order\Address\Validator $addressValidator
3850
* @param array $data
3951
*/
4052
public function __construct(
@@ -43,12 +55,16 @@ public function __construct(
4355
\Magento\Customer\Model\Session $customerSession,
4456
\Magento\Customer\Model\Registration $registration,
4557
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
58+
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
59+
\Magento\Sales\Model\Order\Address\Validator $addressValidator,
4660
array $data = []
4761
) {
4862
$this->checkoutSession = $checkoutSession;
4963
$this->customerSession = $customerSession;
5064
$this->registration = $registration;
5165
$this->accountManagement = $accountManagement;
66+
$this->orderRepository = $orderRepository;
67+
$this->addressValidator = $addressValidator;
5268
parent::__construct($context, $data);
5369
}
5470

@@ -81,9 +97,28 @@ public function toHtml()
8197
$this->customerSession->isLoggedIn()
8298
|| !$this->registration->isAllowed()
8399
|| !$this->accountManagement->isEmailAvailable($this->getEmailAddress())
100+
|| !$this->validateAddresses()
84101
) {
85102
return '';
86103
}
87104
return parent::toHtml();
88105
}
106+
107+
/**
108+
* Validate order addresses
109+
*
110+
* @return bool
111+
*/
112+
protected function validateAddresses()
113+
{
114+
$order = $this->orderRepository->get($this->checkoutSession->getLastOrderId());
115+
$addresses = $order->getAddresses();
116+
foreach ($addresses as $address) {
117+
$result = $this->addressValidator->validateForCustomer($address);
118+
if (is_array($result) && !empty($result)) {
119+
return false;
120+
}
121+
}
122+
return true;
123+
}
89124
}

app/code/Magento/Sales/Model/Order/Address/Validator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ public function validate(Address $address)
7575
}
7676

7777
/**
78-
* Validate address attribute for payment operations
78+
* Validate address attribute for customer creation
7979
*
8080
* @return bool|array
8181
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
8282
* @SuppressWarnings(PHPMD.NPathComplexity)
8383
*
8484
* @param Address $address
8585
*/
86-
public function validateForPayment(Address $address)
86+
public function validateForCustomer(Address $address)
8787
{
8888
if ($address->getShouldIgnoreValidation()) {
8989
return true;

dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,4 +2326,9 @@
23262326
['loadPrices', 'Magento\ConfigurableProduct\Model\Resource\Product\Type\Configurable'],
23272327
['_parseVariationPrices', '\Magento\ConfigurableImportExport\Model\Import\Product\Type\Configurable'],
23282328
['_collectSuperDataPrice', '\Magento\ConfigurableImportExport\Model\Import\Product\Type\Configurable'],
2329+
[
2330+
'validateForPayment',
2331+
'Magento\Sales\Model\Order\Address\Validator',
2332+
'Magento\Sales\Model\Order\Address\Validator::validateForCustomer'
2333+
],
23292334
];

0 commit comments

Comments
 (0)