Skip to content

Commit 6904e9f

Browse files
authored
LYNX-643 Save in address book does not work when using setBillingAddressOnCart GQL's same_as_shipping field
1 parent 87dc83b commit 6904e9f

File tree

4 files changed

+308
-33
lines changed

4 files changed

+308
-33
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Plugin;
9+
10+
use Exception;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Quote\Model\CustomerManagement as Subject;
13+
use Magento\Customer\Api\AddressRepositoryInterface;
14+
use Magento\Quote\Model\Quote;
15+
16+
class CustomerManagementPlugin
17+
{
18+
/**
19+
* CustomerManagementPlugin Constructor
20+
*
21+
* @param AddressRepositoryInterface $addressRepository
22+
*/
23+
public function __construct(
24+
private readonly AddressRepositoryInterface $addressRepository,
25+
) {
26+
}
27+
28+
/**
29+
* Save shipping address in address book if same as billing
30+
*
31+
* @param Subject $subject
32+
* @param Quote $quote
33+
* @return array
34+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
35+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
36+
* @throws LocalizedException
37+
*/
38+
public function beforePopulateCustomerInfo(
39+
Subject $subject,
40+
Quote $quote
41+
): array {
42+
try {
43+
$shippingAddress = $quote->getShippingAddress();
44+
45+
if ($shippingAddress->getSaveInAddressBook()
46+
&& $shippingAddress->getQuoteId()
47+
&& $shippingAddress->getSameAsBilling()
48+
) {
49+
$shippingAddressData = $shippingAddress->exportCustomerAddress();
50+
$shippingAddressData->setCustomerId($quote->getCustomerId());
51+
$this->addressRepository->save($shippingAddressData);
52+
$quote->addCustomerAddress($shippingAddressData);
53+
$shippingAddress->setCustomerAddressData($shippingAddressData);
54+
$shippingAddress->setCustomerAddressId($shippingAddressData->getId());
55+
}
56+
} catch (Exception $e) {
57+
throw new LocalizedException(__($e->getMessage()));
58+
}
59+
60+
return [$quote];
61+
}
62+
}

app/code/Magento/QuoteGraphQl/etc/graphql/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@
9292
</argument>
9393
</arguments>
9494
</type>
95+
<type name="Magento\Quote\Model\CustomerManagement">
96+
<plugin name="billing_address_save_same_as_shipping"
97+
type="Magento\QuoteGraphQl\Plugin\CustomerManagementPlugin"/>
98+
</type>
9599
</config>

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetBillingAddressOnCartTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -1871,7 +1871,7 @@ public function testSetBillingAddressAndPlaceOrder()
18711871
$searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create();
18721872
$addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems();
18731873

1874-
self::assertCount(1, $addresses);
1874+
self::assertCount(2, $addresses);
18751875
self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
18761876
foreach ($addresses as $address) {
18771877
$this->customerAddressRepository->delete($address);
@@ -1953,7 +1953,7 @@ public function testSetBillingAddressWithDefaultValueOfSaveInAddressBookAndPlace
19531953
$searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create();
19541954
$addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems();
19551955

1956-
$this->assertCount(1, $addresses);
1956+
$this->assertCount(2, $addresses);
19571957
$this->assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
19581958
foreach ($addresses as $address) {
19591959
$this->customerAddressRepository->delete($address);

0 commit comments

Comments
 (0)