3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Quote \Model ;
7
9
10
+ use Magento \Customer \Api \AddressRepositoryInterface ;
11
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
12
+ use Magento \Customer \Model \Session ;
13
+ use Magento \Framework \Exception \InputException ;
14
+ use Magento \Framework \Exception \LocalizedException ;
8
15
use Magento \Framework \Exception \NoSuchEntityException ;
9
16
use Magento \Quote \Api \Data \AddressInterface ;
10
17
use Magento \Quote \Api \Data \CartInterface ;
17
24
class QuoteAddressValidator
18
25
{
19
26
/**
20
- * Address factory.
21
- *
22
- * @var \Magento\Customer\Api\AddressRepositoryInterface
27
+ * @var AddressRepositoryInterface
23
28
*/
24
- protected $ addressRepository ;
29
+ protected AddressRepositoryInterface $ addressRepository ;
25
30
26
31
/**
27
- * Customer repository object.
28
- *
29
- * @var \Magento\Customer\Api\CustomerRepositoryInterface
32
+ * @var CustomerRepositoryInterface
30
33
*/
31
- protected $ customerRepository ;
34
+ protected CustomerRepositoryInterface $ customerRepository ;
32
35
33
36
/**
37
+ * @var Session
34
38
* @deprecated 101.1.1 This class is not a part of HTML presentation layer and should not use sessions.
35
- *
36
- * @var \Magento\Customer\Model\Session
39
+ * @see Session
37
40
*/
38
- protected $ customerSession ;
41
+ protected Session $ customerSession ;
39
42
40
43
/**
41
44
* Constructs a quote shipping address validator service object.
42
45
*
43
- * @param \Magento\Customer\Api\ AddressRepositoryInterface $addressRepository
44
- * @param \Magento\Customer\Api\ CustomerRepositoryInterface $customerRepository Customer repository.
45
- * @param \Magento\Customer\Model\ Session $customerSession
46
+ * @param AddressRepositoryInterface $addressRepository
47
+ * @param CustomerRepositoryInterface $customerRepository Customer repository.
48
+ * @param Session $customerSession
46
49
*/
47
50
public function __construct (
48
- \ Magento \ Customer \ Api \ AddressRepositoryInterface $ addressRepository ,
49
- \ Magento \ Customer \ Api \ CustomerRepositoryInterface $ customerRepository ,
50
- \ Magento \ Customer \ Model \ Session $ customerSession
51
+ AddressRepositoryInterface $ addressRepository ,
52
+ CustomerRepositoryInterface $ customerRepository ,
53
+ Session $ customerSession
51
54
) {
52
55
$ this ->addressRepository = $ addressRepository ;
53
56
$ this ->customerRepository = $ customerRepository ;
@@ -58,9 +61,9 @@ public function __construct(
58
61
* Validate address.
59
62
*
60
63
* @param AddressInterface $address
61
- * @param int|null $customerId Cart belongs to
64
+ * @param int|null $customerId
62
65
* @return void
63
- * @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer .
66
+ * @throws LocalizedException The specified customer ID or address ID is not valid .
64
67
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
65
68
*/
66
69
private function doValidate (AddressInterface $ address , ?int $ customerId ): void
@@ -106,30 +109,55 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
106
109
/**
107
110
* Validates the fields in a specified address data object.
108
111
*
109
- * @param \Magento\Quote\Api\Data\ AddressInterface $addressData The address data object.
112
+ * @param AddressInterface $addressData The address data object.
110
113
* @return bool
111
- * @throws \Magento\Framework\Exception\ InputException The specified address belongs to another customer.
112
- * @throws NoSuchEntityException The specified customer ID or address ID is not valid.
114
+ * @throws InputException The specified address belongs to another customer.
115
+ * @throws NoSuchEntityException|LocalizedException The specified customer ID or address ID is not valid.
113
116
*/
114
- public function validate (AddressInterface $ addressData )
117
+ public function validate (AddressInterface $ addressData ): bool
115
118
{
116
119
$ this ->doValidate ($ addressData , $ addressData ->getCustomerId ());
117
120
118
121
return true ;
119
122
}
120
123
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
+ $ size = $ cart ->getAddressesCollection ()->getSize ();
137
+ $ old = $ cart ->getAddressesCollection ()->getItemById ($ address ->getId ());
138
+ if ($ old === null && $ size > 0 ) {
139
+ throw new NoSuchEntityException (
140
+ __ ('Invalid quote address id %1 ' , $ address ->getId ())
141
+ );
142
+ }
143
+ }
144
+ }
145
+
121
146
/**
122
147
* Validate address to be used for cart.
123
148
*
124
149
* @param CartInterface $cart
125
150
* @param AddressInterface $address
126
151
* @return void
127
- * @throws \Magento\Framework\Exception\ InputException The specified address belongs to another customer.
128
- * @throws NoSuchEntityException The specified customer ID or address ID is not valid.
152
+ * @throws InputException The specified address belongs to another customer.
153
+ * @throws NoSuchEntityException|LocalizedException The specified customer ID or address ID is not valid.
129
154
*/
130
155
public function validateForCart (CartInterface $ cart , AddressInterface $ address ): void
131
156
{
132
- $ this ->doValidate ($ address , $ cart ->getCustomerIsGuest () ? null : $ cart ->getCustomer ()->getId ());
157
+ if ($ cart ->getCustomerIsGuest ()) {
158
+ $ this ->doValidateForGuestQuoteAddress ($ address , $ cart );
159
+ }
160
+ $ this ->doValidate ($ address , $ cart ->getCustomerIsGuest () ? null : (int ) $ cart ->getCustomer ()->getId ());
133
161
}
134
162
135
163
/**
0 commit comments