Skip to content

Commit c5a4692

Browse files
author
Prabhu Ram
committed
MC-31586: Customer address is duplicated after setBillingAddressOnCart GraphQL mutation.
- reverting changes from magento/graphql-ce#873 - Added functional tests
1 parent e396c01 commit c5a4692

File tree

5 files changed

+182
-151
lines changed

5 files changed

+182
-151
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/Address/SaveQuoteAddressToCustomerAddressBook.php

Lines changed: 0 additions & 104 deletions
This file was deleted.

app/code/Magento/QuoteGraphQl/Model/Cart/GetShippingAddress.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1414
use Magento\Quote\Model\Quote\Address;
15-
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;
1615

1716
/**
1817
* Get shipping address
@@ -24,21 +23,13 @@ class GetShippingAddress
2423
*/
2524
private $quoteAddressFactory;
2625

27-
/**
28-
* @var SaveQuoteAddressToCustomerAddressBook
29-
*/
30-
private $saveQuoteAddressToCustomerAddressBook;
31-
3226
/**
3327
* @param QuoteAddressFactory $quoteAddressFactory
34-
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
3528
*/
3629
public function __construct(
37-
QuoteAddressFactory $quoteAddressFactory,
38-
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
30+
QuoteAddressFactory $quoteAddressFactory
3931
) {
4032
$this->quoteAddressFactory = $quoteAddressFactory;
41-
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
4233
}
4334

4435
/**
@@ -96,25 +87,15 @@ private function createShippingAddress(
9687

9788
if (null === $customerAddressId) {
9889
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
99-
100-
// need to save address only for registered user and if save_in_address_book = true
101-
if (0 !== $customerId
102-
&& isset($addressInput['save_in_address_book'])
103-
&& (bool)$addressInput['save_in_address_book'] === true
104-
) {
105-
$this->saveQuoteAddressToCustomerAddressBook->execute($shippingAddress, $customerId);
106-
}
10790
} else {
10891
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
10992
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
11093
}
111-
11294
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
11395
(int)$customerAddressId,
11496
$customerId
11597
);
11698
}
117-
11899
return $shippingAddress;
119100
}
120101
}

app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Magento\GraphQl\Model\Query\ContextInterface;
1414
use Magento\Quote\Api\Data\CartInterface;
1515
use Magento\Quote\Model\Quote\Address;
16-
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;
1716

1817
/**
1918
* Set billing address for a specified shopping cart
@@ -30,24 +29,16 @@ class SetBillingAddressOnCart
3029
*/
3130
private $assignBillingAddressToCart;
3231

33-
/**
34-
* @var SaveQuoteAddressToCustomerAddressBook
35-
*/
36-
private $saveQuoteAddressToCustomerAddressBook;
37-
3832
/**
3933
* @param QuoteAddressFactory $quoteAddressFactory
4034
* @param AssignBillingAddressToCart $assignBillingAddressToCart
41-
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
4235
*/
4336
public function __construct(
4437
QuoteAddressFactory $quoteAddressFactory,
45-
AssignBillingAddressToCart $assignBillingAddressToCart,
46-
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
38+
AssignBillingAddressToCart $assignBillingAddressToCart
4739
) {
4840
$this->quoteAddressFactory = $quoteAddressFactory;
4941
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
50-
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
5142
}
5243

5344
/**
@@ -90,7 +81,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
9081
);
9182
}
9283

93-
$billingAddress = $this->createBillingAddress($context, $customerAddressId, $addressInput, $sameAsShipping);
84+
$billingAddress = $this->createBillingAddress($context, $customerAddressId, $addressInput);
9485

9586
$this->assignBillingAddressToCart->execute($cart, $billingAddress, $sameAsShipping);
9687
}
@@ -101,7 +92,6 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
10192
* @param ContextInterface $context
10293
* @param int|null $customerAddressId
10394
* @param array $addressInput
104-
* @param bool $sameAsShipping
10595
* @return Address
10696
* @throws GraphQlAuthorizationException
10797
* @throws GraphQlInputException
@@ -110,21 +100,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
110100
private function createBillingAddress(
111101
ContextInterface $context,
112102
?int $customerAddressId,
113-
?array $addressInput,
114-
$sameAsShipping
103+
?array $addressInput
115104
): Address {
116105
if (null === $customerAddressId) {
117106
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
118-
119-
$customerId = $context->getUserId();
120-
// need to save address only for registered user and if save_in_address_book = true
121-
// and address is not same as shipping
122-
if (0 !== $customerId
123-
&& isset($addressInput['save_in_address_book'])
124-
&& (bool)$addressInput['save_in_address_book'] && !$sameAsShipping
125-
) {
126-
$this->saveQuoteAddressToCustomerAddressBook->execute($billingAddress, $customerId);
127-
}
128107
} else {
129108
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
130109
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
@@ -136,15 +115,13 @@ private function createBillingAddress(
136115
);
137116
}
138117
$errors = $billingAddress->validate();
139-
140118
if (true !== $errors) {
141119
$e = new GraphQlInputException(__('Billing address errors'));
142120
foreach ($errors as $error) {
143121
$e->addError(new GraphQlInputException($error));
144122
}
145123
throw $e;
146124
}
147-
148125
return $billingAddress;
149126
}
150127
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ public function testSetNewBillingAddressWithSaveInAddressBook()
858858
$searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create();
859859
$addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems();
860860

861-
self::assertCount(1, $addresses);
861+
self::assertCount(0, $addresses);
862862
self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
863863

864864
$cartResponse = $response['setBillingAddressOnCart']['cart'];

0 commit comments

Comments
 (0)