Skip to content

Commit 0d8dd04

Browse files
committed
ACP2E-1117: CustomerAddressId not set for new billing address
1 parent 0c38d57 commit 0d8dd04

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function isEqual(?AddressInterface $shippingAddress, ?AddressInterface $b
4040
(int)$billingAddress->getCustomerAddressId());
4141
} else {
4242
$quoteShippingAddressData = $shippingAddress->getData();
43-
$billingAddressData = $billingAddress->getData();
43+
$billingAddressData = ($billingAddress !== null) ? $billingAddress->getData() : null;
4444
if (!empty($quoteShippingAddressData) && !empty($billingAddressData)) {
4545
$billingKeys = array_flip(array_keys($billingAddressData));
4646
$shippingData = array_intersect_key($quoteShippingAddressData, $billingKeys);

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Exception\CouldNotSaveException;
1515
use Magento\Framework\Exception\LocalizedException;
1616
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Quote\Api\Data\AddressInterface;
1718
use Magento\Quote\Model\Quote;
1819

1920
/**
@@ -168,7 +169,6 @@ public function savePaymentInformationAndPlaceOrder(
168169
* @inheritdoc
169170
*
170171
* @throws LocalizedException
171-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
172172
*/
173173
public function savePaymentInformation(
174174
$cartId,
@@ -196,17 +196,12 @@ public function savePaymentInformation(
196196
$quote->removeAddress($quote->getBillingAddress()->getId());
197197
$quote->setBillingAddress($billingAddress);
198198
$quote->setDataChanges(true);
199+
$this->processShippingIfSameAsBilling($quote, $billingAddress);
199200
$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());
210205
}
211206
}
212207
}
@@ -245,29 +240,35 @@ private function getLogger()
245240
* Save shipping address information
246241
*
247242
* @param Quote $quote
243+
* @param AddressInterface|null $billingAddress
248244
* @return void
249245
* @throws LocalizedException
250246
*/
251-
private function saveNewShippingAddress(Quote $quote): void
247+
private function processShippingIfSameAsBilling(Quote $quote, ?AddressInterface $billingAddress): void
252248
{
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());
264271
}
265272
}
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());
272273
}
273274
}

0 commit comments

Comments
 (0)