14
14
use Magento \Customer \Model \Session as CustomerSession ;
15
15
use Magento \Customer \Model \Url as CustomerUrlManager ;
16
16
use Magento \Eav \Api \AttributeOptionManagementInterface ;
17
+ use Magento \Framework \Api \CustomAttributesDataInterface ;
17
18
use Magento \Framework \App \Config \ScopeConfigInterface ;
18
19
use Magento \Framework \App \Http \Context as HttpContext ;
19
20
use Magento \Framework \App \ObjectManager ;
22
23
use Magento \Framework \UrlInterface ;
23
24
use Magento \Quote \Api \CartItemRepositoryInterface as QuoteItemRepository ;
24
25
use Magento \Quote \Api \CartTotalRepositoryInterface ;
26
+ use Magento \Quote \Api \Data \AddressInterface ;
25
27
use Magento \Quote \Api \ShippingMethodManagementInterface as ShippingMethodManager ;
26
28
use Magento \Quote \Model \QuoteIdMaskFactory ;
27
29
use Magento \Store \Model \ScopeInterface ;
30
+ use Magento \Ui \Component \Form \Element \Multiline ;
28
31
29
32
/**
30
33
* Default Config Provider
@@ -272,16 +275,28 @@ public function __construct(
272
275
*
273
276
* @return array|mixed
274
277
* @throws \Magento\Framework\Exception\NoSuchEntityException
278
+ * @throws \Magento\Framework\Exception\LocalizedException
275
279
*/
276
280
public function getConfig ()
277
281
{
278
- $ quoteId = $ this ->checkoutSession ->getQuote ()->getId ();
282
+ $ quote = $ this ->checkoutSession ->getQuote ();
283
+ $ quoteId = $ quote ->getId ();
284
+ $ email = $ quote ->getShippingAddress ()->getEmail ();
279
285
$ output ['formKey ' ] = $ this ->formKey ->getFormKey ();
280
286
$ output ['customerData ' ] = $ this ->getCustomerData ();
281
287
$ output ['quoteData ' ] = $ this ->getQuoteData ();
282
288
$ output ['quoteItemData ' ] = $ this ->getQuoteItemData ();
283
289
$ output ['isCustomerLoggedIn ' ] = $ this ->isCustomerLoggedIn ();
284
290
$ output ['selectedShippingMethod ' ] = $ this ->getSelectedShippingMethod ();
291
+ if ($ email && !$ this ->isCustomerLoggedIn ()) {
292
+ $ shippingAddressFromData = $ this ->getAddressFromData ($ quote ->getShippingAddress ());
293
+ $ billingAddressFromData = $ this ->getAddressFromData ($ quote ->getBillingAddress ());
294
+ $ output ['shippingAddressFromData ' ] = $ shippingAddressFromData ;
295
+ if ($ shippingAddressFromData != $ billingAddressFromData ) {
296
+ $ output ['billingAddressFromData ' ] = $ billingAddressFromData ;
297
+ }
298
+ $ output ['validatedEmailValue ' ] = $ email ;
299
+ }
285
300
$ output ['storeCode ' ] = $ this ->getStoreCode ();
286
301
$ output ['isGuestCheckoutAllowed ' ] = $ this ->isGuestCheckoutAllowed ();
287
302
$ output ['isCustomerLoginRequired ' ] = $ this ->isCustomerLoginRequired ();
@@ -293,11 +308,11 @@ public function getConfig()
293
308
$ output ['staticBaseUrl ' ] = $ this ->getStaticBaseUrl ();
294
309
$ output ['priceFormat ' ] = $ this ->localeFormat ->getPriceFormat (
295
310
null ,
296
- $ this -> checkoutSession -> getQuote () ->getQuoteCurrencyCode ()
311
+ $ quote ->getQuoteCurrencyCode ()
297
312
);
298
313
$ output ['basePriceFormat ' ] = $ this ->localeFormat ->getPriceFormat (
299
314
null ,
300
- $ this -> checkoutSession -> getQuote () ->getBaseCurrencyCode ()
315
+ $ quote ->getBaseCurrencyCode ()
301
316
);
302
317
$ output ['postCodes ' ] = $ this ->postCodesConfig ->getPostCodes ();
303
318
$ output ['imageData ' ] = $ this ->imageProvider ->getImages ($ quoteId );
@@ -528,6 +543,38 @@ private function getSelectedShippingMethod()
528
543
return $ shippingMethodData ;
529
544
}
530
545
546
+ /**
547
+ * Create address data appropriate to fill checkout address form
548
+ *
549
+ * @param AddressInterface $address
550
+ * @return array
551
+ * @throws \Magento\Framework\Exception\LocalizedException
552
+ */
553
+ private function getAddressFromData (AddressInterface $ address )
554
+ {
555
+ $ addressData = [];
556
+ $ attributesMetadata = $ this ->addressMetadata ->getAllAttributesMetadata ();
557
+ foreach ($ attributesMetadata as $ attributeMetadata ) {
558
+ if (!$ attributeMetadata ->isVisible ()) {
559
+ continue ;
560
+ }
561
+ $ attributeCode = $ attributeMetadata ->getAttributeCode ();
562
+ $ attributeData = $ address ->getData ($ attributeCode );
563
+ if ($ attributeData ) {
564
+ if ($ attributeMetadata ->getFrontendInput () === Multiline::NAME ) {
565
+ $ attributeData = \is_array ($ attributeData ) ? $ attributeData : explode ("\n" , $ attributeData );
566
+ $ attributeData = (object )$ attributeData ;
567
+ }
568
+ if ($ attributeMetadata ->isUserDefined ()) {
569
+ $ addressData [CustomAttributesDataInterface::CUSTOM_ATTRIBUTES ][$ attributeCode ] = $ attributeData ;
570
+ continue ;
571
+ }
572
+ $ addressData [$ attributeCode ] = $ attributeData ;
573
+ }
574
+ }
575
+ return $ addressData ;
576
+ }
577
+
531
578
/**
532
579
* Retrieve store code
533
580
*
0 commit comments