16
16
use Magento \Framework \Validator \Exception as ValidatorException ;
17
17
use Magento \Framework \Validator \Factory as ValidatorFactory ;
18
18
use Magento \Quote \Model \Quote as QuoteEntity ;
19
+ use Magento \Customer \Api \Data \RegionInterfaceFactory ;
20
+ use Magento \Customer \Api \Data \AddressInterface ;
21
+ use Magento \Quote \Model \Quote \Address as QuoteAddress ;
19
22
20
23
/**
21
24
* Class Customer
@@ -52,12 +55,18 @@ class CustomerManagement
52
55
*/
53
56
private $ customerAddressFactory ;
54
57
58
+ /**
59
+ * @var RegionInterfaceFactory
60
+ */
61
+ private $ regionFactory ;
62
+
55
63
/**
56
64
* CustomerManagement constructor.
57
65
* @param CustomerRepository $customerRepository
58
66
* @param CustomerAddressRepository $customerAddressRepository
59
67
* @param AccountManagement $accountManagement
60
68
* @param AddressInterfaceFactory $customerAddressFactory
69
+ * @param RegionInterfaceFactory $regionFactory
61
70
* @param ValidatorFactory|null $validatorFactory
62
71
* @param AddressFactory|null $addressFactory
63
72
*/
@@ -66,13 +75,15 @@ public function __construct(
66
75
CustomerAddressRepository $ customerAddressRepository ,
67
76
AccountManagement $ accountManagement ,
68
77
AddressInterfaceFactory $ customerAddressFactory ,
78
+ RegionInterfaceFactory $ regionFactory ,
69
79
?ValidatorFactory $ validatorFactory = null ,
70
80
?AddressFactory $ addressFactory = null
71
81
) {
72
82
$ this ->customerRepository = $ customerRepository ;
73
83
$ this ->customerAddressRepository = $ customerAddressRepository ;
74
84
$ this ->accountManagement = $ accountManagement ;
75
85
$ this ->customerAddressFactory = $ customerAddressFactory ;
86
+ $ this ->regionFactory = $ regionFactory ;
76
87
$ this ->validatorFactory = $ validatorFactory ?: ObjectManager::getInstance ()
77
88
->get (ValidatorFactory::class);
78
89
$ this ->addressFactory = $ addressFactory ?: ObjectManager::getInstance ()
@@ -161,17 +172,7 @@ public function validateAddresses(QuoteEntity $quote)
161
172
}
162
173
if (empty ($ addresses ) && $ quote ->getCustomerIsGuest ()) {
163
174
$ billingAddress = $ quote ->getBillingAddress ();
164
- $ customerAddress = $ this ->customerAddressFactory ->create ();
165
- $ customerAddress ->setFirstname ($ billingAddress ->getFirstname ());
166
- $ customerAddress ->setMiddlename ($ billingAddress ?->getMiddlename());
167
- $ customerAddress ->setLastname ($ billingAddress ->getLastname ());
168
- $ customerAddress ->setStreet ($ billingAddress ->getStreet ());
169
- $ customerAddress ->setCity ($ billingAddress ->getCity ());
170
- $ customerAddress ->setPostcode ($ billingAddress ->getPostcode ());
171
- $ customerAddress ->setTelephone ($ billingAddress ->getTelephone ());
172
- $ customerAddress ->setCountryId ($ billingAddress ->getCountryId ());
173
- $ customerAddress ->setCustomAttributes ($ billingAddress ->getCustomAttributes ());
174
- $ addresses [] = $ customerAddress ;
175
+ $ addresses [] = $ this ->createCustomerAddressFromBilling ($ billingAddress );
175
176
}
176
177
foreach ($ addresses as $ address ) {
177
178
$ validator = $ this ->validatorFactory ->createValidator ('customer_address ' , 'save ' );
@@ -186,4 +187,43 @@ public function validateAddresses(QuoteEntity $quote)
186
187
}
187
188
}
188
189
}
190
+
191
+ /**
192
+ * Creates guest customer address from a billing address.
193
+ *
194
+ * @param QuoteAddress $billingAddress
195
+ * @return AddressInterface
196
+ */
197
+ private function createCustomerAddressFromBilling (QuoteAddress $ billingAddress ): AddressInterface
198
+ {
199
+ $ customerAddress = $ this ->customerAddressFactory ->create ();
200
+ $ customerAddress ->setPrefix ($ billingAddress ?->getPrefix());
201
+ $ customerAddress ->setFirstname ($ billingAddress ->getFirstname ());
202
+ $ customerAddress ->setMiddlename ($ billingAddress ?->getMiddlename());
203
+ $ customerAddress ->setLastname ($ billingAddress ->getLastname ());
204
+ $ customerAddress ->setSuffix ($ billingAddress ?->getSuffix());
205
+ $ customerAddress ->setCompany ($ billingAddress ?->getCompany());
206
+ $ customerAddress ->setStreet ($ billingAddress ->getStreet ());
207
+ $ customerAddress ->setCountryId ($ billingAddress ->getCountryId ());
208
+ $ customerAddress ->setCity ($ billingAddress ->getCity ());
209
+ $ customerAddress ->setPostcode ($ billingAddress ->getPostcode ());
210
+ $ customerAddress ->setTelephone ($ billingAddress ->getTelephone ());
211
+ $ customerAddress ->setFax ($ billingAddress ?->getFax());
212
+ $ customerAddress ->setVatId ($ billingAddress ?->getVatId());
213
+ $ regionData = $ billingAddress ->getRegion ();
214
+ if (is_array ($ regionData )) {
215
+ $ region = $ this ->regionFactory ->create ();
216
+ $ region ->setRegion ($ regionData ['region ' ] ?? null );
217
+ $ region ->setRegionCode ($ regionData ['region_code ' ] ?? null );
218
+ $ region ->setRegionId ($ regionData ['region_id ' ] ?? null );
219
+ } elseif (is_string ($ regionData )) {
220
+ $ region = $ this ->regionFactory ->create ();
221
+ $ region ->setRegion ($ regionData );
222
+ } else {
223
+ $ region = null ;
224
+ }
225
+ $ customerAddress ->setRegion ($ region );
226
+ $ customerAddress ->setCustomAttributes ($ billingAddress ->getCustomAttributes ());
227
+ return $ customerAddress ;
228
+ }
189
229
}
0 commit comments