|
8 | 8 |
|
9 | 9 | use Magento\Framework\App\ObjectManager;
|
10 | 10 | use Magento\Framework\Exception\InputException;
|
| 11 | +use Magento\Framework\Exception\NoSuchEntityException; |
11 | 12 | use Magento\Quote\Api\BillingAddressManagementInterface;
|
12 | 13 | use Magento\Quote\Api\Data\AddressInterface;
|
| 14 | +use Magento\Quote\Api\Data\CartInterface; |
13 | 15 | use Psr\Log\LoggerInterface as Logger;
|
14 | 16 |
|
15 | 17 | /**
|
@@ -77,12 +79,8 @@ public function assign($cartId, AddressInterface $address, $useForShipping = fal
|
77 | 79 | /** @var \Magento\Quote\Model\Quote $quote */
|
78 | 80 | $quote = $this->quoteRepository->getActive($cartId);
|
79 | 81 |
|
80 |
| - // reset the address id, if it doesn't belong to quote. |
81 |
| - $old = $quote->getAddressesCollection()->getItemById($address->getId()) |
82 |
| - ?? $quote->getBillingAddress(); |
83 |
| - if ($old !== null) { |
84 |
| - $address->setId($old->getId()); |
85 |
| - } |
| 82 | + // validate the address |
| 83 | + $this->validateAddress($quote, $address); |
86 | 84 |
|
87 | 85 | $address->setCustomerId($quote->getCustomerId());
|
88 | 86 | $quote->removeAddress($quote->getBillingAddress()->getId());
|
@@ -121,4 +119,24 @@ private function getShippingAddressAssignment()
|
121 | 119 | }
|
122 | 120 | return $this->shippingAddressAssignment;
|
123 | 121 | }
|
| 122 | + |
| 123 | + /** |
| 124 | + * Validate address to be used for cart. |
| 125 | + * |
| 126 | + * @param CartInterface $cart |
| 127 | + * @param AddressInterface $address |
| 128 | + * @return void |
| 129 | + * @throws InputException The specified address belongs to another customer. |
| 130 | + * @throws NoSuchEntityException The specified customer ID or address ID is not valid. |
| 131 | + */ |
| 132 | + public function validateAddress(CartInterface $cart, AddressInterface $address): void |
| 133 | + { |
| 134 | + // check if address belongs to quote. |
| 135 | + if ($address->getId() !== null) { |
| 136 | + $old = $cart->getAddressesCollection()->getItemById($address->getId()); |
| 137 | + if ($old === null) { |
| 138 | + throw new InputException(__('Invalid address')); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
124 | 142 | }
|
0 commit comments