|
43 | 43 | use Magento\Sales\Api\Data\OrderInterfaceFactory as OrderFactory;
|
44 | 44 | use Magento\Sales\Api\OrderManagementInterface as OrderManagement;
|
45 | 45 | use Magento\Store\Model\StoreManagerInterface;
|
| 46 | +use Magento\Customer\Model\AddressRegistry; |
46 | 47 |
|
47 | 48 | /**
|
48 | 49 | * Class for managing quote
|
@@ -183,6 +184,11 @@ class QuoteManagement implements CartManagementInterface, ResetAfterRequestInter
|
183 | 184 | */
|
184 | 185 | private $cartMutex;
|
185 | 186 |
|
| 187 | + /** |
| 188 | + * @var AddressRegistry |
| 189 | + */ |
| 190 | + private $addressRegistry; |
| 191 | + |
186 | 192 | /**
|
187 | 193 | * @param EventManager $eventManager
|
188 | 194 | * @param SubmitQuoteValidator $submitQuoteValidator
|
@@ -210,6 +216,7 @@ class QuoteManagement implements CartManagementInterface, ResetAfterRequestInter
|
210 | 216 | * @param RemoteAddress|null $remoteAddress
|
211 | 217 | * @param LockManagerInterface $lockManager
|
212 | 218 | * @param CartMutexInterface|null $cartMutex
|
| 219 | + * @param AddressRegistry|null $addressRegistry |
213 | 220 | * @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
214 | 221 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
215 | 222 | */
|
@@ -239,7 +246,8 @@ public function __construct(
|
239 | 246 | RequestInterface $request = null,
|
240 | 247 | RemoteAddress $remoteAddress = null,
|
241 | 248 | LockManagerInterface $lockManager = null,
|
242 |
| - ?CartMutexInterface $cartMutex = null |
| 249 | + ?CartMutexInterface $cartMutex = null, |
| 250 | + ?AddressRegistry $addressRegistry = null |
243 | 251 | ) {
|
244 | 252 | $this->eventManager = $eventManager;
|
245 | 253 | $this->submitQuoteValidator = $submitQuoteValidator;
|
@@ -271,6 +279,8 @@ public function __construct(
|
271 | 279 | ->get(RemoteAddress::class);
|
272 | 280 | $this->cartMutex = $cartMutex
|
273 | 281 | ?? ObjectManager::getInstance()->get(CartMutexInterface::class);
|
| 282 | + $this->addressRegistry = $addressRegistry |
| 283 | + ?? ObjectManager::getInstance()->get(AddressRegistry::class); |
274 | 284 | }
|
275 | 285 |
|
276 | 286 | /**
|
@@ -696,7 +706,9 @@ protected function _prepareCustomerQuote($quote)
|
696 | 706 | }
|
697 | 707 | //save here new customer address
|
698 | 708 | $shippingAddress->setCustomerId($quote->getCustomerId());
|
699 |
| - $this->addressRepository->save($shippingAddress); |
| 709 | + if ($this->validateAddress((int) $shippingAddress->getId()) === true) { |
| 710 | + $this->addressRepository->save($shippingAddress); |
| 711 | + } |
700 | 712 | $quote->addCustomerAddress($shippingAddress);
|
701 | 713 | $shipping->setCustomerAddressData($shippingAddress);
|
702 | 714 | $this->addressesToSync[] = $shippingAddress->getId();
|
@@ -728,7 +740,9 @@ protected function _prepareCustomerQuote($quote)
|
728 | 740 | $billingAddress->setIsDefaultBilling(true);
|
729 | 741 | }
|
730 | 742 | $billingAddress->setCustomerId($quote->getCustomerId());
|
731 |
| - $this->addressRepository->save($billingAddress); |
| 743 | + if ($this->validateAddress((int) $billingAddress->getId()) === true) { |
| 744 | + $this->addressRepository->save($billingAddress); |
| 745 | + } |
732 | 746 | $quote->addCustomerAddress($billingAddress);
|
733 | 747 | $billing->setCustomerAddressData($billingAddress);
|
734 | 748 | $this->addressesToSync[] = $billingAddress->getId();
|
@@ -791,4 +805,17 @@ public function _resetState(): void
|
791 | 805 | {
|
792 | 806 | $this->addressesToSync = [];
|
793 | 807 | }
|
| 808 | + |
| 809 | + /** |
| 810 | + * Validate address |
| 811 | + * |
| 812 | + * @param int|null $addressId |
| 813 | + * @return array|bool |
| 814 | + * @throws NoSuchEntityException |
| 815 | + */ |
| 816 | + private function validateAddress(?int $addressId): bool|array |
| 817 | + { |
| 818 | + $addressModel = $this->addressRegistry->retrieve($addressId); |
| 819 | + return $addressModel->validate(); |
| 820 | + } |
794 | 821 | }
|
0 commit comments