|
14 | 14 | use Magento\Framework\Exception\CouldNotSaveException;
|
15 | 15 | use Magento\Framework\Exception\LocalizedException;
|
16 | 16 | use Magento\Quote\Api\CartRepositoryInterface;
|
| 17 | +use Magento\Quote\Api\Data\AddressInterface; |
17 | 18 | use Magento\Quote\Model\Quote;
|
18 | 19 |
|
19 | 20 | /**
|
@@ -168,7 +169,6 @@ public function savePaymentInformationAndPlaceOrder(
|
168 | 169 | * @inheritdoc
|
169 | 170 | *
|
170 | 171 | * @throws LocalizedException
|
171 |
| - * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
172 | 172 | */
|
173 | 173 | public function savePaymentInformation(
|
174 | 174 | $cartId,
|
@@ -196,17 +196,12 @@ public function savePaymentInformation(
|
196 | 196 | $quote->removeAddress($quote->getBillingAddress()->getId());
|
197 | 197 | $quote->setBillingAddress($billingAddress);
|
198 | 198 | $quote->setDataChanges(true);
|
| 199 | + $this->processShippingIfSameAsBilling($quote, $billingAddress); |
199 | 200 | $shippingAddress = $quote->getShippingAddress();
|
200 |
| - if ($shippingAddress) { |
201 |
| - if ((bool)$shippingAddress->getSameAsBilling() |
202 |
| - || $this->addressComparator->isEqual($shippingAddress, $billingAddress)) { |
203 |
| - $this->saveNewShippingAddress($quote); |
204 |
| - } |
205 |
| - if ($shippingAddress->getShippingMethod()) { |
206 |
| - $shippingRate = $shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod()); |
207 |
| - if ($shippingRate) { |
208 |
| - $shippingAddress->setLimitCarrier($shippingRate->getCarrier()); |
209 |
| - } |
| 201 | + if ($shippingAddress && $shippingAddress->getShippingMethod()) { |
| 202 | + $shippingRate = $shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod()); |
| 203 | + if ($shippingRate) { |
| 204 | + $shippingAddress->setLimitCarrier($shippingRate->getCarrier()); |
210 | 205 | }
|
211 | 206 | }
|
212 | 207 | }
|
@@ -245,29 +240,35 @@ private function getLogger()
|
245 | 240 | * Save shipping address information
|
246 | 241 | *
|
247 | 242 | * @param Quote $quote
|
| 243 | + * @param AddressInterface|null $billingAddress |
248 | 244 | * @return void
|
249 | 245 | * @throws LocalizedException
|
250 | 246 | */
|
251 |
| - private function saveNewShippingAddress(Quote $quote): void |
| 247 | + private function processShippingIfSameAsBilling(Quote $quote, ?AddressInterface $billingAddress): void |
252 | 248 | {
|
253 |
| - $quote->getShippingAddress()->setSameAsBilling(1); |
254 |
| - $shippingAddressData = $quote->getShippingAddress()->exportCustomerAddress(); |
255 |
| - |
256 |
| - $customer = $quote->getCustomer(); |
257 |
| - $hasDefaultBilling = (bool)$customer->getDefaultBilling(); |
258 |
| - $hasDefaultShipping = (bool)$customer->getDefaultShipping(); |
259 |
| - if (!$hasDefaultShipping) { |
260 |
| - //Make provided address as default shipping address |
261 |
| - $shippingAddressData->setIsDefaultShipping(true); |
262 |
| - if (!$hasDefaultBilling && !$quote->getBillingAddress()->getSaveInAddressBook()) { |
263 |
| - $shippingAddressData->setIsDefaultBilling(true); |
| 249 | + $shippingAddress = $quote->getShippingAddress(); |
| 250 | + if ((bool)$shippingAddress->getSameAsBilling() |
| 251 | + || $this->addressComparator->isEqual($shippingAddress, $billingAddress)) { |
| 252 | + $shippingAddress->setSameAsBilling(1); |
| 253 | + if ($shippingAddress->getSaveInAddressBook()) { |
| 254 | + $shippingAddressData = $shippingAddress->exportCustomerAddress(); |
| 255 | + $customer = $quote->getCustomer(); |
| 256 | + $hasDefaultBilling = (bool)$customer->getDefaultBilling(); |
| 257 | + $hasDefaultShipping = (bool)$customer->getDefaultShipping(); |
| 258 | + if (!$hasDefaultShipping) { |
| 259 | + //Make provided address as default shipping address |
| 260 | + $shippingAddressData->setIsDefaultShipping(true); |
| 261 | + if (!$hasDefaultBilling && !$quote->getBillingAddress()->getSaveInAddressBook()) { |
| 262 | + $shippingAddressData->setIsDefaultBilling(true); |
| 263 | + } |
| 264 | + } |
| 265 | + $shippingAddressData->setCustomerId($quote->getCustomerId()); |
| 266 | + $this->addressRepository->save($shippingAddressData); |
| 267 | + $quote->addCustomerAddress($shippingAddressData); |
| 268 | + $shippingAddress->setCustomerAddressData($shippingAddressData); |
| 269 | + $shippingAddress->setCustomerAddressId($shippingAddressData->getId()); |
| 270 | + $quote->getBillingAddress()->setCustomerAddressId($shippingAddressData->getId()); |
264 | 271 | }
|
265 | 272 | }
|
266 |
| - $shippingAddressData->setCustomerId($quote->getCustomerId()); |
267 |
| - $this->addressRepository->save($shippingAddressData); |
268 |
| - $quote->addCustomerAddress($shippingAddressData); |
269 |
| - $quote->getShippingAddress()->setCustomerAddressData($shippingAddressData); |
270 |
| - $quote->getShippingAddress()->setCustomerAddressId($shippingAddressData->getId()); |
271 |
| - $quote->getBillingAddress()->setCustomerAddressId($shippingAddressData->getId()); |
272 | 273 | }
|
273 | 274 | }
|
0 commit comments