@@ -59,15 +59,15 @@ public function __construct(
59
59
* @param int|null $customerId Cart belongs to
60
60
* @return void
61
61
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
62
- * @throws \Magento\Framework\Exception\ NoSuchEntityException The specified customer ID or address ID is not valid.
62
+ * @throws NoSuchEntityException The specified customer ID or address ID is not valid.
63
63
*/
64
64
private function doValidate (AddressInterface $ address , ?int $ customerId ): void
65
65
{
66
66
//validate customer id
67
67
if ($ customerId ) {
68
68
$ customer = $ this ->customerRepository ->getById ($ customerId );
69
69
if (!$ customer ->getId ()) {
70
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (
70
+ throw new NoSuchEntityException (
71
71
__ ('Invalid customer id %1 ' , $ customerId )
72
72
);
73
73
}
@@ -76,15 +76,15 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
76
76
if ($ address ->getCustomerAddressId ()) {
77
77
//Existing address cannot belong to a guest
78
78
if (!$ customerId ) {
79
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (
79
+ throw new NoSuchEntityException (
80
80
__ ('Invalid customer address id %1 ' , $ address ->getCustomerAddressId ())
81
81
);
82
82
}
83
83
//Validating address ID
84
84
try {
85
85
$ this ->addressRepository ->getById ($ address ->getCustomerAddressId ());
86
86
} catch (NoSuchEntityException $ e ) {
87
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (
87
+ throw new NoSuchEntityException (
88
88
__ ('Invalid address id %1 ' , $ address ->getId ())
89
89
);
90
90
}
@@ -94,7 +94,7 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
94
94
return $ address ->getId ();
95
95
}, $ this ->customerRepository ->getById ($ customerId )->getAddresses ());
96
96
if (!in_array ($ address ->getCustomerAddressId (), $ applicableAddressIds )) {
97
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (
97
+ throw new NoSuchEntityException (
98
98
__ ('Invalid customer address id %1 ' , $ address ->getCustomerAddressId ())
99
99
);
100
100
}
@@ -107,7 +107,7 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
107
107
* @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object.
108
108
* @return bool
109
109
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
110
- * @throws \Magento\Framework\Exception\ NoSuchEntityException The specified customer ID or address ID is not valid.
110
+ * @throws NoSuchEntityException The specified customer ID or address ID is not valid.
111
111
*/
112
112
public function validate (AddressInterface $ addressData )
113
113
{
@@ -123,10 +123,31 @@ public function validate(AddressInterface $addressData)
123
123
* @param AddressInterface $address
124
124
* @return void
125
125
* @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer.
126
- * @throws \Magento\Framework\Exception\ NoSuchEntityException The specified customer ID or address ID is not valid.
126
+ * @throws NoSuchEntityException The specified customer ID or address ID is not valid.
127
127
*/
128
128
public function validateForCart (CartInterface $ cart , AddressInterface $ address ): void
129
129
{
130
130
$ this ->doValidate ($ address , $ cart ->getCustomerIsGuest () ? null : $ cart ->getCustomer ()->getId ());
131
131
}
132
+
133
+ /**
134
+ * Validate address id to be used for cart.
135
+ *
136
+ * @param CartInterface $cart
137
+ * @param AddressInterface $address
138
+ * @return void
139
+ * @throws NoSuchEntityException The specified customer ID or address ID is not valid.
140
+ */
141
+ public function validateAddress (CartInterface $ cart , AddressInterface $ address ): void
142
+ {
143
+ // check if address belongs to quote.
144
+ if ($ address ->getId () !== null ) {
145
+ $ old = $ cart ->getAddressesCollection ()->getItemById ($ address ->getId ());
146
+ if ($ old === null ) {
147
+ throw new NoSuchEntityException (
148
+ __ ('Invalid quote address id %1 ' , $ address ->getId ())
149
+ );
150
+ }
151
+ }
152
+ }
132
153
}
0 commit comments