Skip to content

Commit 86fe531

Browse files
AC-7058: Guest Checkout REST API Fix
1 parent 58a0fa1 commit 86fe531

File tree

2 files changed

+78
-43
lines changed

2 files changed

+78
-43
lines changed

app/code/Magento/Checkout/Model/ShippingInformationManagement.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Checkout\Model;
89

@@ -39,60 +40,62 @@ class ShippingInformationManagement implements ShippingInformationManagementInte
3940
/**
4041
* @var PaymentMethodManagementInterface
4142
*/
42-
protected $paymentMethodManagement;
43+
protected PaymentMethodManagementInterface $paymentMethodManagement;
4344

4445
/**
4546
* @var PaymentDetailsFactory
4647
*/
47-
protected $paymentDetailsFactory;
48+
protected PaymentDetailsFactory $paymentDetailsFactory;
4849

4950
/**
5051
* @var CartTotalRepositoryInterface
5152
*/
52-
protected $cartTotalsRepository;
53+
protected CartTotalRepositoryInterface $cartTotalsRepository;
5354

5455
/**
5556
* @var CartRepositoryInterface
5657
*/
57-
protected $quoteRepository;
58-
58+
protected CartRepositoryInterface $quoteRepository;
5959
/**
6060
* @var Logger
6161
*/
62-
protected $logger;
62+
protected Logger $logger;
6363

6464
/**
6565
* @var QuoteAddressValidator
6666
*/
67-
protected $addressValidator;
67+
protected QuoteAddressValidator $addressValidator;
6868

6969
/**
7070
* @var AddressRepositoryInterface
7171
* @deprecated 100.2.0
72+
* @see AddressRepositoryInterface
7273
*/
73-
protected $addressRepository;
74+
protected AddressRepositoryInterface $addressRepository;
7475

7576
/**
7677
* @var ScopeConfigInterface
7778
* @deprecated 100.2.0
79+
* @see ScopeConfigInterface
7880
*/
79-
protected $scopeConfig;
81+
protected ScopeConfigInterface $scopeConfig;
8082

8183
/**
8284
* @var TotalsCollector
8385
* @deprecated 100.2.0
86+
* @see TotalsCollector
8487
*/
85-
protected $totalsCollector;
88+
protected TotalsCollector $totalsCollector;
8689

8790
/**
8891
* @var CartExtensionFactory
8992
*/
90-
private $cartExtensionFactory;
93+
private CartExtensionFactory $cartExtensionFactory;
9194

9295
/**
9396
* @var ShippingAssignmentFactory
9497
*/
95-
protected $shippingAssignmentFactory;
98+
protected ShippingAssignmentFactory $shippingAssignmentFactory;
9699

97100
/**
98101
* @var ShippingFactory
@@ -262,8 +265,11 @@ protected function validateQuote(Quote $quote): void
262265
* @param string $method
263266
* @return CartInterface
264267
*/
265-
private function prepareShippingAssignment(CartInterface $quote, AddressInterface $address, $method): CartInterface
266-
{
268+
private function prepareShippingAssignment(
269+
CartInterface $quote,
270+
AddressInterface $address,
271+
string $method
272+
): CartInterface {
267273
$cartExtension = $quote->getExtensionAttributes();
268274
if ($cartExtension === null) {
269275
$cartExtension = $this->cartExtensionFactory->create();

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

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace 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;
815
use Magento\Framework\Exception\NoSuchEntityException;
916
use Magento\Quote\Api\Data\AddressInterface;
1017
use Magento\Quote\Api\Data\CartInterface;
@@ -17,35 +24,33 @@
1724
class 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.
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.
39+
* @see Session
3540
*/
36-
protected $customerSession;
41+
protected Session $customerSession;
3742

3843
/**
3944
* Constructs a quote shipping address validator service object.
4045
*
41-
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
42-
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository Customer repository.
43-
* @param \Magento\Customer\Model\Session $customerSession
46+
* @param AddressRepositoryInterface $addressRepository
47+
* @param CustomerRepositoryInterface $customerRepository Customer repository.
48+
* @param Session $customerSession
4449
*/
4550
public function __construct(
46-
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
47-
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
48-
\Magento\Customer\Model\Session $customerSession
51+
AddressRepositoryInterface $addressRepository,
52+
CustomerRepositoryInterface $customerRepository,
53+
Session $customerSession
4954
) {
5055
$this->addressRepository = $addressRepository;
5156
$this->customerRepository = $customerRepository;
@@ -56,18 +61,18 @@ public function __construct(
5661
* Validate address.
5762
*
5863
* @param AddressInterface $address
59-
* @param int|null $customerId Cart belongs to
64+
* @param int|null $customerId
6065
* @return void
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.
66+
* @throws LocalizedException The specified customer ID or address ID is not valid.
67+
* @throws NoSuchEntityException The specified customer ID or address ID is not valid.
6368
*/
6469
private function doValidate(AddressInterface $address, ?int $customerId): void
6570
{
6671
//validate customer id
6772
if ($customerId) {
6873
$customer = $this->customerRepository->getById($customerId);
6974
if (!$customer->getId()) {
70-
throw new \Magento\Framework\Exception\NoSuchEntityException(
75+
throw new NoSuchEntityException(
7176
__('Invalid customer id %1', $customerId)
7277
);
7378
}
@@ -76,15 +81,15 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
7681
if ($address->getCustomerAddressId()) {
7782
//Existing address cannot belong to a guest
7883
if (!$customerId) {
79-
throw new \Magento\Framework\Exception\NoSuchEntityException(
84+
throw new NoSuchEntityException(
8085
__('Invalid customer address id %1', $address->getCustomerAddressId())
8186
);
8287
}
8388
//Validating address ID
8489
try {
8590
$this->addressRepository->getById($address->getCustomerAddressId());
8691
} catch (NoSuchEntityException $e) {
87-
throw new \Magento\Framework\Exception\NoSuchEntityException(
92+
throw new NoSuchEntityException(
8893
__('Invalid address id %1', $address->getId())
8994
);
9095
}
@@ -94,7 +99,7 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
9499
return $address->getId();
95100
}, $this->customerRepository->getById($customerId)->getAddresses());
96101
if (!in_array($address->getCustomerAddressId(), $applicableAddressIds)) {
97-
throw new \Magento\Framework\Exception\NoSuchEntityException(
102+
throw new NoSuchEntityException(
98103
__('Invalid customer address id %1', $address->getCustomerAddressId())
99104
);
100105
}
@@ -104,29 +109,53 @@ private function doValidate(AddressInterface $address, ?int $customerId): void
104109
/**
105110
* Validates the fields in a specified address data object.
106111
*
107-
* @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object.
112+
* @param AddressInterface $addressData The address data object.
108113
* @return bool
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.
114+
* @throws InputException The specified address belongs to another customer.
115+
* @throws NoSuchEntityException|LocalizedException The specified customer ID or address ID is not valid.
111116
*/
112-
public function validate(AddressInterface $addressData)
117+
public function validate(AddressInterface $addressData): bool
113118
{
114119
$this->doValidate($addressData, $addressData->getCustomerId());
115120

116121
return true;
117122
}
118123

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->getAddressesCollection()->getItemById($address->getId());
137+
if ($old === null) {
138+
throw new NoSuchEntityException(
139+
__('Invalid quote address id %1', $address->getId())
140+
);
141+
}
142+
}
143+
}
144+
119145
/**
120146
* Validate address to be used for cart.
121147
*
122148
* @param CartInterface $cart
123149
* @param AddressInterface $address
124150
* @return void
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.
151+
* @throws InputException The specified address belongs to another customer.
152+
* @throws NoSuchEntityException|LocalizedException The specified customer ID or address ID is not valid.
127153
*/
128154
public function validateForCart(CartInterface $cart, AddressInterface $address): void
129155
{
130-
$this->doValidate($address, $cart->getCustomerIsGuest() ? null : $cart->getCustomer()->getId());
156+
if ($cart->getCustomerIsGuest()) {
157+
$this->doValidateForGuestQuoteAddress($address, $cart);
158+
}
159+
$this->doValidate($address, $cart->getCustomerIsGuest() ? null : (int) $cart->getCustomer()->getId());
131160
}
132161
}

0 commit comments

Comments
 (0)