Skip to content

Commit 01e3f19

Browse files
authored
Merge pull request #5509 from magento-honey-badgers/MC-32655-address
[honey] MC-32655: (GraphQL) Set default value of save_in_address_book to true when no value is specified
2 parents 42cf94a + b2d3f13 commit 01e3f19

File tree

6 files changed

+219
-21
lines changed

6 files changed

+219
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
namespace Magento\QuoteGraphQl\Model\Cart;
99

1010
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
11-
use Magento\GraphQl\Model\Query\ContextInterface;
1211
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1312
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
13+
use Magento\GraphQl\Model\Query\ContextInterface;
1414
use Magento\Quote\Model\Quote\Address;
1515

1616
/**

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,18 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
5656
{
5757
$customerAddressId = $billingAddressInput['customer_address_id'] ?? null;
5858
$addressInput = $billingAddressInput['address'] ?? null;
59+
60+
if (!$customerAddressId && !isset($billingAddressInput['address']['save_in_address_book']) && $addressInput) {
61+
$addressInput['save_in_address_book'] = true;
62+
}
63+
5964
// Need to keep this for BC of `use_for_shipping` field
6065
$sameAsShipping = isset($billingAddressInput['use_for_shipping'])
6166
? (bool)$billingAddressInput['use_for_shipping'] : false;
6267
$sameAsShipping = isset($billingAddressInput['same_as_shipping'])
6368
? (bool)$billingAddressInput['same_as_shipping'] : $sameAsShipping;
6469

65-
if (null === $customerAddressId && null === $addressInput) {
66-
throw new GraphQlInputException(
67-
__('The billing address must contain either "customer_address_id" or "address".')
68-
);
69-
}
70-
71-
if ($customerAddressId && $addressInput) {
72-
throw new GraphQlInputException(
73-
__('The billing address cannot contain "customer_address_id" and "address" at the same time.')
74-
);
75-
}
70+
$this->checkForInputExceptions($billingAddressInput);
7671

7772
$addresses = $cart->getAllShippingAddresses();
7873
if ($sameAsShipping && count($addresses) > 1) {
@@ -86,6 +81,31 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
8681
$this->assignBillingAddressToCart->execute($cart, $billingAddress, $sameAsShipping);
8782
}
8883

84+
/**
85+
* Check for the input exceptions
86+
*
87+
* @param array $billingAddressInput
88+
* @throws GraphQlInputException
89+
*/
90+
private function checkForInputExceptions(
91+
?array $billingAddressInput
92+
) {
93+
$customerAddressId = $billingAddressInput['customer_address_id'] ?? null;
94+
$addressInput = $billingAddressInput['address'] ?? null;
95+
96+
if (null === $customerAddressId && null === $addressInput) {
97+
throw new GraphQlInputException(
98+
__('The billing address must contain either "customer_address_id" or "address".')
99+
);
100+
}
101+
102+
if ($customerAddressId && $addressInput) {
103+
throw new GraphQlInputException(
104+
__('The billing address cannot contain "customer_address_id" and "address" at the same time.')
105+
);
106+
}
107+
}
108+
89109
/**
90110
* Create billing address
91111
*

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1111
use Magento\GraphQl\Model\Query\ContextInterface;
1212
use Magento\Quote\Api\Data\CartInterface;
13-
use Magento\Quote\Model\Quote\Address;
1413

1514
/**
1615
* Set single shipping address for a specified shopping cart
@@ -49,7 +48,12 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
4948
__('You cannot specify multiple shipping addresses.')
5049
);
5150
}
52-
$shippingAddressInput = current($shippingAddressesInput);
51+
$shippingAddressInput = current($shippingAddressesInput) ?? [];
52+
$customerAddressId = $shippingAddressInput['customer_address_id'] ?? null;
53+
54+
if (!$customerAddressId && !isset($shippingAddressInput['address']['save_in_address_book'])) {
55+
$shippingAddressInput['address']['save_in_address_book'] = true;
56+
}
5357

5458
$shippingAddress = $this->getShippingAddress->execute($context, $shippingAddressInput);
5559

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ input CartAddressInput {
112112
postcode: String
113113
country_code: String!
114114
telephone: String!
115-
save_in_address_book: Boolean
115+
save_in_address_book: Boolean @doc(description: "Determines whether to save the address in the customer's address book. The default value is true")
116116
}
117117

118118
input SetShippingMethodsOnCartInput {

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

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,88 @@ public function testSetBillingAddressAndPlaceOrder()
954954
}
955955
}
956956

957+
/**
958+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
959+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
960+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
961+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
962+
*/
963+
public function testSetBillingAddressWithDefaultValueOfSaveInAddressBookAndPlaceOrder()
964+
{
965+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
966+
$query = <<<QUERY
967+
mutation {
968+
setBillingAddressOnCart(
969+
input: {
970+
cart_id: "$maskedQuoteId"
971+
billing_address: {
972+
same_as_shipping: true
973+
address: {
974+
firstname: "test firstname"
975+
lastname: "test lastname"
976+
company: "test company"
977+
street: ["test street 1", "test street 2"]
978+
city: "test city"
979+
region: "AZ"
980+
postcode: "88776"
981+
country_code: "US"
982+
telephone: "88776655"
983+
}
984+
}
985+
}
986+
) {
987+
cart {
988+
billing_address {
989+
firstname
990+
lastname
991+
company
992+
street
993+
city
994+
postcode
995+
telephone
996+
country {
997+
code
998+
label
999+
}
1000+
__typename
1001+
}
1002+
}
1003+
}
1004+
}
1005+
QUERY;
1006+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
1007+
$this->graphQlMutation(
1008+
$this->getSetShippingMethodsQuery($maskedQuoteId, 'flatrate', 'flatrate'),
1009+
[],
1010+
'',
1011+
$this->getHeaderMap()
1012+
);
1013+
$this->graphQlMutation(
1014+
$this->getSetPaymentMethodQuery(
1015+
$maskedQuoteId,
1016+
'checkmo'
1017+
),
1018+
[],
1019+
'',
1020+
$this->getHeaderMap()
1021+
);
1022+
$this->graphQlMutation(
1023+
$this->getPlaceOrderQuery($maskedQuoteId),
1024+
[],
1025+
'',
1026+
$this->getHeaderMap()
1027+
);
1028+
$customer = $this->customerRepository->get('customer@example.com');
1029+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create();
1030+
$addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems();
1031+
1032+
$this->assertCount(1, $addresses);
1033+
$this->assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
1034+
foreach ($addresses as $address) {
1035+
$this->customerAddressRepository->delete($address);
1036+
}
1037+
}
1038+
9571039
/**
9581040
* @magentoApiDataFixture Magento/Customer/_files/customer.php
9591041
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
@@ -1208,9 +1290,9 @@ private function getSetShippingMethodsQuery(
12081290
): string {
12091291
return <<<QUERY
12101292
mutation {
1211-
setShippingMethodsOnCart(input:
1293+
setShippingMethodsOnCart(input:
12121294
{
1213-
cart_id: "$maskedQuoteId",
1295+
cart_id: "$maskedQuoteId",
12141296
shipping_methods: [{
12151297
carrier_code: "$shippingCarrierCode"
12161298
method_code: "$shippingMethodCode"
@@ -1251,7 +1333,7 @@ private function getSetPaymentMethodQuery(
12511333
payment_method: {
12521334
code: "$methodCode"
12531335
}
1254-
}) {
1336+
}) {
12551337
cart {
12561338
selected_payment_method {
12571339
code

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

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,98 @@ public function testSetNewShippingAddressAndPlaceOrder()
10461046
}
10471047
}
10481048

1049+
/**
1050+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
1051+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
1052+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
1053+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
1054+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
1055+
*/
1056+
public function testSetNewShippingAddressWithDefaultValueOfSaveInAddressBookAndPlaceOrder()
1057+
{
1058+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
1059+
$query = <<<QUERY
1060+
mutation {
1061+
setShippingAddressesOnCart(
1062+
input: {
1063+
cart_id: "$maskedQuoteId"
1064+
shipping_addresses: [
1065+
{
1066+
address: {
1067+
firstname: "test firstname"
1068+
lastname: "test lastname"
1069+
company: "test company"
1070+
street: ["test street 1", "test street 2"]
1071+
city: "test city"
1072+
region: "AZ"
1073+
postcode: "887766"
1074+
country_code: "US"
1075+
telephone: "88776655"
1076+
}
1077+
customer_notes: "Test note"
1078+
}
1079+
]
1080+
}
1081+
) {
1082+
cart {
1083+
shipping_addresses {
1084+
firstname
1085+
lastname
1086+
company
1087+
street
1088+
city
1089+
postcode
1090+
telephone
1091+
country {
1092+
code
1093+
label
1094+
}
1095+
__typename
1096+
customer_notes
1097+
}
1098+
}
1099+
}
1100+
}
1101+
QUERY;
1102+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
1103+
$this->graphQlMutation(
1104+
$this->getSetShippingMethodsQuery($maskedQuoteId, 'flatrate', 'flatrate'),
1105+
[],
1106+
'',
1107+
$this->getHeaderMap()
1108+
);
1109+
$this->graphQlMutation(
1110+
$this->getSetPaymentMethodQuery(
1111+
$maskedQuoteId,
1112+
'checkmo'
1113+
),
1114+
[],
1115+
'',
1116+
$this->getHeaderMap()
1117+
);
1118+
$this->graphQlMutation(
1119+
$this->getPlaceOrderQuery($maskedQuoteId),
1120+
[],
1121+
'',
1122+
$this->getHeaderMap()
1123+
);
1124+
$customer = $this->customerRepository->get('customer@example.com');
1125+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create();
1126+
$addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems();
1127+
1128+
$this->assertCount(1, $addresses);
1129+
$this->assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
1130+
1131+
$cartResponse = $response['setShippingAddressesOnCart']['cart'];
1132+
$this->assertArrayHasKey('shipping_addresses', $cartResponse);
1133+
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
1134+
$this->assertNewShippingAddressFields($shippingAddressResponse);
1135+
1136+
foreach ($addresses as $address) {
1137+
$this->customerAddressRepository->delete($address);
1138+
}
1139+
}
1140+
10491141
/**
10501142
* @magentoApiDataFixture Magento/Customer/_files/customer.php
10511143
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
@@ -1201,9 +1293,9 @@ private function getSetShippingMethodsQuery(
12011293
): string {
12021294
return <<<QUERY
12031295
mutation {
1204-
setShippingMethodsOnCart(input:
1296+
setShippingMethodsOnCart(input:
12051297
{
1206-
cart_id: "$maskedQuoteId",
1298+
cart_id: "$maskedQuoteId",
12071299
shipping_methods: [{
12081300
carrier_code: "$shippingCarrierCode"
12091301
method_code: "$shippingMethodCode"
@@ -1244,7 +1336,7 @@ private function getSetPaymentMethodQuery(
12441336
payment_method: {
12451337
code: "$methodCode"
12461338
}
1247-
}) {
1339+
}) {
12481340
cart {
12491341
selected_payment_method {
12501342
code

0 commit comments

Comments
 (0)