10
10
use Magento \Customer \Api \AddressRepositoryInterface ;
11
11
use Magento \Customer \Api \Data \AddressInterface ;
12
12
use Magento \Customer \Api \Data \AddressInterfaceFactory ;
13
+ use Magento \Directory \Helper \Data as DirectoryData ;
14
+ use Magento \Framework \Api \DataObjectHelper ;
13
15
use Magento \Framework \Exception \LocalizedException ;
14
16
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
- use Magento \Framework \Api \DataObjectHelper ;
16
17
17
18
/**
18
19
* Create customer address
@@ -38,23 +39,30 @@ class CreateCustomerAddress
38
39
* @var DataObjectHelper
39
40
*/
40
41
private $ dataObjectHelper ;
42
+ /**
43
+ * @var DirectoryData
44
+ */
45
+ private $ directoryData ;
41
46
42
47
/**
43
48
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
44
49
* @param AddressInterfaceFactory $addressFactory
45
50
* @param AddressRepositoryInterface $addressRepository
46
51
* @param DataObjectHelper $dataObjectHelper
52
+ * @param DirectoryData $directoryData
47
53
*/
48
54
public function __construct (
49
55
GetAllowedAddressAttributes $ getAllowedAddressAttributes ,
50
56
AddressInterfaceFactory $ addressFactory ,
51
57
AddressRepositoryInterface $ addressRepository ,
52
- DataObjectHelper $ dataObjectHelper
58
+ DataObjectHelper $ dataObjectHelper ,
59
+ DirectoryData $ directoryData
53
60
) {
54
61
$ this ->getAllowedAddressAttributes = $ getAllowedAddressAttributes ;
55
62
$ this ->addressFactory = $ addressFactory ;
56
63
$ this ->addressRepository = $ addressRepository ;
57
64
$ this ->dataObjectHelper = $ dataObjectHelper ;
65
+ $ this ->directoryData = $ directoryData ;
58
66
}
59
67
60
68
/**
@@ -102,6 +110,13 @@ public function validateData(array $addressData): void
102
110
$ attributes = $ this ->getAllowedAddressAttributes ->execute ();
103
111
$ errorInput = [];
104
112
113
+ //Add error for empty postcode with country with no optional ZIP
114
+ if (!in_array ($ addressData ['country_id ' ], $ this ->directoryData ->getCountriesWithOptionalZip ())
115
+ && (!isset ($ addressData ['postcode ' ]) || empty ($ addressData ['postcode ' ]))
116
+ ) {
117
+ $ errorInput [] = 'postcode ' ;
118
+ }
119
+
105
120
foreach ($ attributes as $ attributeName => $ attributeInfo ) {
106
121
if ($ attributeInfo ->getIsRequired ()
107
122
&& (!isset ($ addressData [$ attributeName ]) || empty ($ addressData [$ attributeName ]))
0 commit comments