Skip to content

Commit 1719ee7

Browse files
committed
AC-11432: Improve address validation
1 parent d606831 commit 1719ee7

File tree

4 files changed

+70
-30
lines changed

4 files changed

+70
-30
lines changed

app/code/Magento/Quote/Model/BillingAddressManagement.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ public function assign($cartId, AddressInterface $address, $useForShipping = fal
7777
{
7878
/** @var \Magento\Quote\Model\Quote $quote */
7979
$quote = $this->quoteRepository->getActive($cartId);
80-
81-
// validate the address
82-
$this->addressValidator->validateWithExistingAddress($quote, $address);
83-
8480
$address->setCustomerId($quote->getCustomerId());
8581
$quote->removeAddress($quote->getBillingAddress()->getId());
8682
$quote->setBillingAddress($address);

app/code/Magento/Quote/Model/QuoteAddressValidator.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,6 @@ public function validate(AddressInterface $addressData): bool
121121
return true;
122122
}
123123

124-
/**
125-
* Validate Quest Address for guest user
126-
*
127-
* @param AddressInterface $address
128-
* @param CartInterface $cart
129-
* @return void
130-
* @throws NoSuchEntityException
131-
*/
132-
private function doValidateForGuestQuoteAddress(AddressInterface $address, CartInterface $cart): void
133-
{
134-
//validate guest cart address
135-
if ($address->getId() !== null) {
136-
$old = $cart->getAddressById($address->getId());
137-
if ($old === false) {
138-
throw new NoSuchEntityException(
139-
__('Invalid quote address id %1', $address->getId())
140-
);
141-
}
142-
}
143-
}
144-
145124
/**
146125
* Validate address to be used for cart.
147126
*
@@ -153,9 +132,6 @@ private function doValidateForGuestQuoteAddress(AddressInterface $address, CartI
153132
*/
154133
public function validateForCart(CartInterface $cart, AddressInterface $address): void
155134
{
156-
if ($cart->getCustomerIsGuest()) {
157-
$this->doValidateForGuestQuoteAddress($address, $cart);
158-
}
159135
$this->doValidate($address, $cart->getCustomerIsGuest() ? null : (int) $cart->getCustomer()->getId());
160136
}
161137

@@ -171,8 +147,8 @@ public function validateWithExistingAddress(CartInterface $cart, AddressInterfac
171147
{
172148
// check if address belongs to quote.
173149
if ($address->getId() !== null) {
174-
$old = $cart->getAddressesCollection()->getItemById($address->getId());
175-
if ($old === null) {
150+
$old = $cart->getAddressById($address->getId());
151+
if ($old === false) {
176152
throw new NoSuchEntityException(
177153
__('Invalid quote address id %1', $address->getId())
178154
);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Plugin;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Quote\Model\Quote;
12+
use Magento\Quote\Api\Data\AddressInterface;
13+
use Magento\Quote\Model\QuoteAddressValidator;
14+
15+
/**
16+
* Quote address plugin
17+
*/
18+
class QuoteAddress
19+
{
20+
/**
21+
* @var QuoteAddressValidator
22+
*/
23+
protected QuoteAddressValidator $addressValidator;
24+
25+
/**
26+
* @param QuoteAddressValidator $addressValidator
27+
*/
28+
public function __construct(
29+
QuoteAddressValidator $addressValidator
30+
) {
31+
$this->addressValidator = $addressValidator;
32+
}
33+
34+
/**
35+
* Validate address before setting billing address
36+
*
37+
* @param Quote $subject
38+
* @param AddressInterface|null $address
39+
* @return array
40+
* @throws NoSuchEntityException
41+
*/
42+
public function beforeSetBillingAddress(Quote $subject, AddressInterface $address = null): array
43+
{
44+
if ($address !== null) {
45+
$this->addressValidator->validateWithExistingAddress($subject, $address);
46+
}
47+
48+
return [$address];
49+
}
50+
51+
/**
52+
* Validate address before setting shipping address
53+
*
54+
* @param Quote $subject
55+
* @param AddressInterface|null $address
56+
* @return array
57+
* @throws NoSuchEntityException
58+
*/
59+
public function beforeSetShippingAddress(Quote $subject, AddressInterface $address = null): array
60+
{
61+
if ($address !== null) {
62+
$this->addressValidator->validateWithExistingAddress($subject, $address);
63+
}
64+
65+
return [$address];
66+
}
67+
}

app/code/Magento/Quote/etc/webapi_rest/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</type>
1919
<type name="Magento\Quote\Model\Quote">
2020
<plugin name="updateQuoteStoreId" type="Magento\Quote\Model\Quote\Plugin\UpdateQuoteStoreId" />
21+
<plugin name="validateQuoteAddress" type="Magento\Quote\Plugin\QuoteAddress" />
2122
</type>
2223
<type name="Magento\Webapi\Controller\Rest\ParamsOverrider">
2324
<plugin name="validateQuoteData" type="Magento\Quote\Plugin\Webapi\Controller\Rest\ValidateQuoteData" sortOrder="1" disabled="false" />

0 commit comments

Comments
 (0)