Skip to content

Commit 0cd1367

Browse files
ENGCOM-6255: #1029 Add Postcode as required depending of the country Mutation createCustomerAddress #1031
- Merge Pull Request magento/graphql-ce#1031 from osrecio/graphql-ce:1029-postcode-required-customerCreate - Merged commits: 1. 676cd1b 2. 127835e 3. 891e5a4
2 parents 4e3da15 + 891e5a4 commit 0cd1367

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
use Magento\Customer\Api\AddressRepositoryInterface;
1111
use Magento\Customer\Api\Data\AddressInterface;
1212
use Magento\Customer\Api\Data\AddressInterfaceFactory;
13+
use Magento\Directory\Helper\Data as DirectoryData;
14+
use Magento\Framework\Api\DataObjectHelper;
1315
use Magento\Framework\Exception\LocalizedException;
1416
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15-
use Magento\Framework\Api\DataObjectHelper;
1617

1718
/**
1819
* Create customer address
@@ -38,23 +39,30 @@ class CreateCustomerAddress
3839
* @var DataObjectHelper
3940
*/
4041
private $dataObjectHelper;
42+
/**
43+
* @var DirectoryData
44+
*/
45+
private $directoryData;
4146

4247
/**
4348
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
4449
* @param AddressInterfaceFactory $addressFactory
4550
* @param AddressRepositoryInterface $addressRepository
4651
* @param DataObjectHelper $dataObjectHelper
52+
* @param DirectoryData $directoryData
4753
*/
4854
public function __construct(
4955
GetAllowedAddressAttributes $getAllowedAddressAttributes,
5056
AddressInterfaceFactory $addressFactory,
5157
AddressRepositoryInterface $addressRepository,
52-
DataObjectHelper $dataObjectHelper
58+
DataObjectHelper $dataObjectHelper,
59+
DirectoryData $directoryData
5360
) {
5461
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
5562
$this->addressFactory = $addressFactory;
5663
$this->addressRepository = $addressRepository;
5764
$this->dataObjectHelper = $dataObjectHelper;
65+
$this->directoryData = $directoryData;
5866
}
5967

6068
/**
@@ -102,6 +110,13 @@ public function validateData(array $addressData): void
102110
$attributes = $this->getAllowedAddressAttributes->execute();
103111
$errorInput = [];
104112

113+
//Add error for empty postcode with country with no optional ZIP
114+
if (!$this->directoryData->isZipCodeOptional($addressData['country_id'])
115+
&& (!isset($addressData['postcode']) || empty($addressData['postcode']))
116+
) {
117+
$errorInput[] = 'postcode';
118+
}
119+
105120
foreach ($attributes as $attributeName => $attributeInfo) {
106121
if ($attributeInfo->getIsRequired()
107122
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))

app/code/Magento/CustomerGraphQl/composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
"type": "magento2-module",
55
"require": {
66
"php": "~7.1.3||~7.2.0||~7.3.0",
7-
"magento/module-customer": "*",
87
"magento/module-authorization": "*",
98
"magento/module-customer": "*",
109
"magento/module-eav": "*",
1110
"magento/module-graph-ql": "*",
1211
"magento/module-newsletter": "*",
1312
"magento/module-integration": "*",
1413
"magento/module-store": "*",
15-
"magento/framework": "*"
14+
"magento/framework": "*",
15+
"magento/module-directory": "*"
1616
},
1717
"license": [
1818
"OSL-3.0",

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

+61
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,67 @@ public function testCreateCustomerAddressWithRedundantStreetLine()
358358
$this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
359359
}
360360

361+
/**
362+
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
363+
* @magentoConfigFixture default_store general/country/optional_zip_countries UA
364+
*
365+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
366+
*/
367+
public function testCreateCustomerAddressWithOptionalZipCode()
368+
{
369+
$newAddress = [
370+
'country_code' => 'UA',
371+
'street' => ['Line 1 Street', 'Line 2'],
372+
'company' => 'Company name',
373+
'telephone' => '123456789',
374+
'fax' => '123123123',
375+
'city' => 'City Name',
376+
'firstname' => 'Adam',
377+
'lastname' => 'Phillis',
378+
'middlename' => 'A',
379+
'prefix' => 'Mr.',
380+
'suffix' => 'Jr.',
381+
'vat_id' => '1',
382+
'default_shipping' => true,
383+
'default_billing' => false
384+
];
385+
386+
$mutation
387+
= <<<MUTATION
388+
mutation {
389+
createCustomerAddress(input: {
390+
country_code: {$newAddress['country_code']}
391+
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"]
392+
company: "{$newAddress['company']}"
393+
telephone: "{$newAddress['telephone']}"
394+
fax: "{$newAddress['fax']}"
395+
city: "{$newAddress['city']}"
396+
firstname: "{$newAddress['firstname']}"
397+
lastname: "{$newAddress['lastname']}"
398+
middlename: "{$newAddress['middlename']}"
399+
prefix: "{$newAddress['prefix']}"
400+
suffix: "{$newAddress['suffix']}"
401+
vat_id: "{$newAddress['vat_id']}"
402+
default_shipping: true
403+
default_billing: false
404+
}) {
405+
id
406+
}
407+
}
408+
MUTATION;
409+
410+
$userName = '[email protected]';
411+
$password = 'password';
412+
413+
$response = $this->graphQlMutation(
414+
$mutation,
415+
[],
416+
'',
417+
$this->getCustomerAuthHeaders($userName, $password)
418+
);
419+
$this->assertNotEmpty($response['createCustomerAddress']['id']);
420+
}
421+
361422
/**
362423
* Create new address with invalid input
363424
*

0 commit comments

Comments
 (0)