Skip to content

Commit 34a91dc

Browse files
committed
ACP2E-1117: CustomerAddressId not set for new billing address
1 parent fc35ac4 commit 34a91dc

8 files changed

+152
-689
lines changed
Lines changed: 87 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,66 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
declare(strict_types=1);
6+
declare(strict_types = 1);
77

8-
namespace Magento\Checkout\Plugin\Model;
8+
namespace Magento\Checkout\Model;
99

10-
use Magento\Checkout\Api\PaymentInformationManagementInterface;
1110
use Magento\Customer\Api\AddressRepositoryInterface;
1211
use Magento\Framework\Exception\LocalizedException;
1312
use Magento\Quote\Api\CartRepositoryInterface;
1413
use Magento\Quote\Api\Data\AddressInterface;
1514
use Magento\Quote\Api\Data\PaymentInterface;
1615
use Magento\Quote\Model\Quote;
16+
use Magento\Quote\Model\QuoteIdMaskFactory;
1717

18-
/**
19-
* Class PaymentInformationManagement
20-
*/
21-
class PaymentInformationManagementPlugin
18+
class AddressMapper implements AddressMapperInterface
2219
{
2320
/**
2421
* @var CartRepositoryInterface
2522
*/
26-
private $quoteRepository;
23+
private $cartRepository;
2724

2825
/**
2926
* @var AddressRepositoryInterface
3027
*/
3128
private $addressRepository;
3229

3330
/**
34-
* PaymentInformationManagement constructor
31+
* @var QuoteIdMaskFactory
32+
*/
33+
private $quoteIdMaskFactory;
34+
35+
/**
36+
* AddressMapper constructor
3537
*
36-
* @param CartRepositoryInterface $quoteRepository
38+
* @param CartRepositoryInterface $cartRepository
3739
* @param AddressRepositoryInterface $addressRepository
40+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
3841
*/
3942
public function __construct(
40-
CartRepositoryInterface $quoteRepository,
41-
AddressRepositoryInterface $addressRepository
43+
CartRepositoryInterface $cartRepository,
44+
AddressRepositoryInterface $addressRepository,
45+
QuoteIdMaskFactory $quoteIdMaskFactory
4246
) {
43-
$this->quoteRepository = $quoteRepository;
47+
$this->cartRepository = $cartRepository;
4448
$this->addressRepository = $addressRepository;
49+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
4550
}
4651

4752
/**
48-
* Disable order submitting for preview
49-
*
50-
* @param PaymentInformationManagementInterface $subject
51-
* @param int $cartId
52-
* @param PaymentInterface $paymentMethod
53-
* @param AddressInterface|null $billingAddress
54-
* @return void
53+
* @inheritDoc
5554
* @throws LocalizedException
56-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
57-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5855
*/
59-
public function beforeSavePaymentInformationAndPlaceOrder(
60-
PaymentInformationManagementInterface $subject,
56+
public function customerCheckoutAddressMapper(
6157
int $cartId,
6258
PaymentInterface $paymentMethod,
6359
AddressInterface $billingAddress = null
6460
): void {
6561
/** @var Quote $quote */
66-
$quote = $this->quoteRepository->getActive($cartId);
62+
$quote = $this->cartRepository->getActive($cartId);
6763
$shippingAddress = $quote->getShippingAddress();
6864
$quoteShippingAddressData = $shippingAddress->getData();
69-
$quoteSameAsBilling = (int) $shippingAddress->getSameAsBilling();
65+
$quoteSameAsBilling = (int)$shippingAddress->getSameAsBilling();
7066
$customer = $quote->getCustomer();
7167
$customerId = $customer->getId();
7268
$hasDefaultBilling = $customer->getDefaultBilling();
@@ -76,7 +72,7 @@ public function beforeSavePaymentInformationAndPlaceOrder(
7672
$sameAsBillingFlag = 1;
7773
} elseif (!empty($quoteShippingAddressData) && !empty($billingAddress)) {
7874
$sameAsBillingFlag = $quote->getCustomerId() &&
79-
$this->checkIfShippingNullOrNotSameAsBillingAddress($shippingAddress, $billingAddress);
75+
$this->checkIfShippingAddressMatchesWithBillingAddress($shippingAddress, $billingAddress);
8076
} else {
8177
$sameAsBillingFlag = 0;
8278
}
@@ -97,13 +93,74 @@ public function beforeSavePaymentInformationAndPlaceOrder(
9793
}
9894

9995
/**
100-
* Returns true if shipping address is same as billing or it is undefined
96+
* @inheritDoc
97+
*/
98+
public function guestCheckoutAddressMapper(
99+
string $cartId,
100+
string $email,
101+
PaymentInterface $paymentMethod,
102+
AddressInterface $billingAddress = null
103+
): void {
104+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
105+
/** @var Quote $quote */
106+
$quote = $this->cartRepository->getActive($quoteIdMask->getQuoteId());
107+
$shippingAddress = $quote->getShippingAddress();
108+
109+
if (!empty($billingAddress)) {
110+
$sameAsBillingFlag = $this->checkIfShippingAddressMatchesWithBillingAddress($shippingAddress, $billingAddress);
111+
} else {
112+
$sameAsBillingFlag = 0;
113+
}
114+
115+
if ($sameAsBillingFlag) {
116+
$shippingAddress->setSameAsBilling(1);
117+
}
118+
}
119+
120+
/**
121+
* Process customer shipping address
122+
*
123+
* @param Quote $quote
124+
* @return void
125+
* @throws LocalizedException
126+
*/
127+
private function processCustomerShippingAddress(Quote $quote): void
128+
{
129+
$shippingAddress = $quote->getShippingAddress();
130+
$billingAddress = $quote->getBillingAddress();
131+
132+
$customer = $quote->getCustomer();
133+
$hasDefaultBilling = $customer->getDefaultBilling();
134+
$hasDefaultShipping = $customer->getDefaultShipping();
135+
136+
if ($shippingAddress->getQuoteId()) {
137+
$shippingAddressData = $shippingAddress->exportCustomerAddress();
138+
}
139+
if (isset($shippingAddressData)) {
140+
if (!$hasDefaultShipping) {
141+
//Make provided address as default shipping address
142+
$shippingAddressData->setIsDefaultShipping(true);
143+
if (!$hasDefaultBilling && !$billingAddress->getSaveInAddressBook()) {
144+
$shippingAddressData->setIsDefaultBilling(true);
145+
}
146+
}
147+
//save here new customer address
148+
$shippingAddressData->setCustomerId($quote->getCustomerId());
149+
$this->addressRepository->save($shippingAddressData);
150+
$quote->addCustomerAddress($shippingAddressData);
151+
$shippingAddress->setCustomerAddressData($shippingAddressData);
152+
$shippingAddress->setCustomerAddressId($shippingAddressData->getId());
153+
}
154+
}
155+
156+
/**
157+
* Returns true if shipping address is same as billing, or it is undefined
101158
*
102159
* @param AddressInterface $shippingAddress
103160
* @param AddressInterface $billingAddress
104161
* @return bool
105162
*/
106-
private function checkIfShippingNullOrNotSameAsBillingAddress(
163+
private function checkIfShippingAddressMatchesWithBillingAddress(
107164
AddressInterface $shippingAddress,
108165
AddressInterface $billingAddress
109166
): bool {
@@ -141,7 +198,7 @@ private function convertAddressValueToFlatArray(array $address): array
141198
{
142199
array_walk(
143200
$address,
144-
function (&$value) {
201+
static function (&$value) {
145202
if (is_array($value) && isset($value['value'])) {
146203
if (!is_array($value['value'])) {
147204
$value = (string)$value['value'];
@@ -153,40 +210,4 @@ function (&$value) {
153210
);
154211
return $address;
155212
}
156-
157-
/**
158-
* Process customer shipping address
159-
*
160-
* @param Quote $quote
161-
* @return void
162-
* @throws LocalizedException
163-
*/
164-
private function processCustomerShippingAddress(Quote $quote): void
165-
{
166-
$shippingAddress = $quote->getShippingAddress();
167-
$billingAddress = $quote->getBillingAddress();
168-
169-
$customer = $quote->getCustomer();
170-
$hasDefaultBilling = $customer->getDefaultBilling();
171-
$hasDefaultShipping = $customer->getDefaultShipping();
172-
173-
if ($shippingAddress->getQuoteId()) {
174-
$shippingAddressData = $shippingAddress->exportCustomerAddress();
175-
}
176-
if (isset($shippingAddressData)) {
177-
if (!$hasDefaultShipping) {
178-
//Make provided address as default shipping address
179-
$shippingAddressData->setIsDefaultShipping(true);
180-
if (!$hasDefaultBilling && !$billingAddress->getSaveInAddressBook()) {
181-
$shippingAddressData->setIsDefaultBilling(true);
182-
}
183-
}
184-
//save here new customer address
185-
$shippingAddressData->setCustomerId($quote->getCustomerId());
186-
$this->addressRepository->save($shippingAddressData);
187-
$quote->addCustomerAddress($shippingAddressData);
188-
$shippingAddress->setCustomerAddressData($shippingAddressData);
189-
$shippingAddress->setCustomerAddressId($shippingAddressData->getId());
190-
}
191-
}
192213
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Model;
9+
10+
use Magento\Quote\Api\Data\AddressInterface;
11+
use Magento\Quote\Api\Data\PaymentInterface;
12+
13+
interface AddressMapperInterface
14+
{
15+
/**
16+
* Process customer address information for same as billing
17+
*
18+
* @param int $cartId
19+
* @param PaymentInterface $paymentMethod
20+
* @param AddressInterface|null $billingAddress
21+
* @return void
22+
*/
23+
public function customerCheckoutAddressMapper(
24+
int $cartId,
25+
PaymentInterface $paymentMethod,
26+
AddressInterface $billingAddress = null
27+
): void;
28+
29+
/**
30+
* Process guest address information for same as billing
31+
*
32+
* @param string $cartId
33+
* @param string $email
34+
* @param PaymentInterface $paymentMethod
35+
* @param AddressInterface|null $billingAddress
36+
* @return void
37+
*/
38+
public function guestCheckoutAddressMapper(
39+
string $cartId,
40+
string $email,
41+
PaymentInterface $paymentMethod,
42+
AddressInterface $billingAddress = null
43+
): void;
44+
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPa
7373
*/
7474
private $saveRateLimitDisabled = false;
7575

76+
/**
77+
* @var AddressMapperInterface
78+
*/
79+
private $addressMapper;
80+
7681
/**
7782
* @param \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement
7883
* @param \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement
@@ -82,6 +87,7 @@ class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPa
8287
* @param CartRepositoryInterface $cartRepository
8388
* @param PaymentProcessingRateLimiterInterface|null $paymentsRateLimiter
8489
* @param PaymentSavingRateLimiterInterface|null $savingRateLimiter
90+
* @param AddressMapperInterface|null $addressMapper
8591
* @codeCoverageIgnore
8692
*/
8793
public function __construct(
@@ -92,7 +98,8 @@ public function __construct(
9298
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
9399
CartRepositoryInterface $cartRepository,
94100
?PaymentProcessingRateLimiterInterface $paymentsRateLimiter = null,
95-
?PaymentSavingRateLimiterInterface $savingRateLimiter = null
101+
?PaymentSavingRateLimiterInterface $savingRateLimiter = null,
102+
?AddressMapperInterface $addressMapper = null
96103
) {
97104
$this->billingAddressManagement = $billingAddressManagement;
98105
$this->paymentMethodManagement = $paymentMethodManagement;
@@ -104,6 +111,7 @@ public function __construct(
104111
?? ObjectManager::getInstance()->get(PaymentProcessingRateLimiterInterface::class);
105112
$this->savingRateLimiter = $savingRateLimiter
106113
?? ObjectManager::getInstance()->get(PaymentSavingRateLimiterInterface::class);
114+
$this->addressMapper = $addressMapper ?? ObjectManager::getInstance()->get(AddressMapperInterface::class);
107115
}
108116

109117
/**
@@ -115,6 +123,7 @@ public function savePaymentInformationAndPlaceOrder(
115123
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
116124
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
117125
) {
126+
$this->addressMapper->guestCheckoutAddressMapper($cartId, $email, $paymentMethod, $billingAddress);
118127
$this->paymentsRateLimiter->limit();
119128
try {
120129
//Have to do this hack because of savePaymentInformation() plugins.

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
7171
*/
7272
private $saveRateLimiterDisabled = false;
7373

74+
/**
75+
* @var AddressMapperInterface
76+
*/
77+
private $addressMapper;
78+
7479
/**
7580
* @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
7681
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
@@ -80,6 +85,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
8085
* @param PaymentProcessingRateLimiterInterface|null $paymentRateLimiter
8186
* @param PaymentSavingRateLimiterInterface|null $saveRateLimiter
8287
* @param CartRepositoryInterface|null $cartRepository
88+
* @param AddressMapperInterface|null $addressMapper
8389
* @codeCoverageIgnore
8490
*/
8591
public function __construct(
@@ -90,7 +96,8 @@ public function __construct(
9096
\Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository,
9197
?PaymentProcessingRateLimiterInterface $paymentRateLimiter = null,
9298
?PaymentSavingRateLimiterInterface $saveRateLimiter = null,
93-
?CartRepositoryInterface $cartRepository = null
99+
?CartRepositoryInterface $cartRepository = null,
100+
?AddressMapperInterface $addressMapper = null
94101
) {
95102
$this->billingAddressManagement = $billingAddressManagement;
96103
$this->paymentMethodManagement = $paymentMethodManagement;
@@ -103,6 +110,7 @@ public function __construct(
103110
?? ObjectManager::getInstance()->get(PaymentSavingRateLimiterInterface::class);
104111
$this->cartRepository = $cartRepository
105112
?? ObjectManager::getInstance()->get(CartRepositoryInterface::class);
113+
$this->addressMapper = $addressMapper ?? ObjectManager::getInstance()->get(AddressMapperInterface::class);
106114
}
107115

108116
/**
@@ -113,6 +121,7 @@ public function savePaymentInformationAndPlaceOrder(
113121
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
114122
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
115123
) {
124+
$this->addressMapper->customerCheckoutAddressMapper($cartId, $paymentMethod, $billingAddress);
116125
$this->paymentRateLimiter->limit();
117126
try {
118127
//Have to do this hack because of plugins for savePaymentInformation()

0 commit comments

Comments
 (0)