33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace Magento \Quote \Model ;
79
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 ;
815use Magento \Framework \Exception \NoSuchEntityException ;
916use Magento \Quote \Api \Data \AddressInterface ;
1017use Magento \Quote \Api \Data \CartInterface ;
1724class QuoteAddressValidator
1825{
1926 /**
20- * Address factory.
21- *
22- * @var \Magento\Customer\Api\AddressRepositoryInterface
27+ * @var AddressRepositoryInterface
2328 */
24- protected $ addressRepository ;
29+ protected AddressRepositoryInterface $ addressRepository ;
2530
2631 /**
27- * Customer repository object.
28- *
29- * @var \Magento\Customer\Api\CustomerRepositoryInterface
32+ * @var CustomerRepositoryInterface
3033 */
31- protected $ customerRepository ;
34+ protected CustomerRepositoryInterface $ customerRepository ;
3235
3336 /**
37+ * @var Session
3438 * @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
3740 */
38- protected $ customerSession ;
41+ protected Session $ customerSession ;
3942
4043 /**
4144 * Constructs a quote shipping address validator service object.
4245 *
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
4649 */
4750 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
5154 ) {
5255 $ this ->addressRepository = $ addressRepository ;
5356 $ this ->customerRepository = $ customerRepository ;
@@ -58,9 +61,9 @@ public function __construct(
5861 * Validate address.
5962 *
6063 * @param AddressInterface $address
61- * @param int|null $customerId Cart belongs to
64+ * @param int|null $customerId
6265 * @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 .
6467 * @throws NoSuchEntityException The specified customer ID or address ID is not valid.
6568 */
6669 private function doValidate (AddressInterface $ address , ?int $ customerId ): void
@@ -106,30 +109,55 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
106109 /**
107110 * Validates the fields in a specified address data object.
108111 *
109- * @param \Magento\Quote\Api\Data\ AddressInterface $addressData The address data object.
112+ * @param AddressInterface $addressData The address data object.
110113 * @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.
113116 */
114- public function validate (AddressInterface $ addressData )
117+ public function validate (AddressInterface $ addressData ): bool
115118 {
116119 $ this ->doValidate ($ addressData , $ addressData ->getCustomerId ());
117120
118121 return true ;
119122 }
120123
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+
121146 /**
122147 * Validate address to be used for cart.
123148 *
124149 * @param CartInterface $cart
125150 * @param AddressInterface $address
126151 * @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.
129154 */
130155 public function validateForCart (CartInterface $ cart , AddressInterface $ address ): void
131156 {
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 ());
133161 }
134162
135163 /**
0 commit comments