14
14
use Magento \Customer \Api \Data \AddressInterface ;
15
15
use Magento \Customer \Api \Data \RegionInterface ;
16
16
use Magento \Customer \Api \Data \AddressInterfaceFactory as AddressFactory ;
17
- use Magento \Quote \Model \Quote \Address as QuoteAddress ;
18
17
use Magento \Customer \Api \Data \RegionInterfaceFactory as RegionFactory ;
19
18
use Magento \Customer \Api \Data \CustomerInterfaceFactory as CustomerFactory ;
20
19
use Magento \Quote \Api \Data \AddressInterfaceFactory as QuoteAddressFactory ;
20
+ use Magento \Sales \Model \Order \Address as OrderAddress ;
21
21
22
22
/**
23
23
* Extract customer data from an order.
@@ -87,8 +87,9 @@ public function __construct(
87
87
}
88
88
89
89
/**
90
- * @param int $orderId
90
+ * Extract customer data from order.
91
91
*
92
+ * @param int $orderId
92
93
* @return CustomerInterface
93
94
*/
94
95
public function extract (int $ orderId ): CustomerInterface
@@ -107,36 +108,45 @@ public function extract(int $orderId): CustomerInterface
107
108
$ order ->getBillingAddress (),
108
109
[]
109
110
);
110
- $ addresses = $ order ->getAddresses ();
111
- foreach ($ addresses as $ address ) {
112
- $ addressData = $ this ->objectCopyService ->copyFieldsetToTarget (
113
- 'order_address ' ,
114
- 'to_customer_address ' ,
115
- $ address ,
116
- []
117
- );
118
- /** @var AddressInterface $customerAddress */
119
- $ customerAddress = $ this ->addressFactory ->create (['data ' => $ addressData ]);
120
- switch ($ address ->getAddressType ()) {
121
- case QuoteAddress::ADDRESS_TYPE_BILLING :
122
- $ customerAddress ->setIsDefaultBilling (true );
123
- break ;
124
- case QuoteAddress::ADDRESS_TYPE_SHIPPING :
125
- $ customerAddress ->setIsDefaultShipping (true );
126
- break ;
111
+
112
+ $ processedAddressData = [];
113
+ $ customerAddresses = [];
114
+ foreach ($ order ->getAddresses () as $ orderAddress ) {
115
+ $ addressData = $ this ->objectCopyService
116
+ ->copyFieldsetToTarget ('order_address ' , 'to_customer_address ' , $ orderAddress , []);
117
+
118
+ $ index = array_search ($ addressData , $ processedAddressData );
119
+ if ($ index === false ) {
120
+ // create new customer address only if it is unique
121
+ $ customerAddress = $ this ->addressFactory ->create (['data ' => $ addressData ]);
122
+ $ customerAddress ->setIsDefaultBilling (false );
123
+ $ customerAddress ->setIsDefaultBilling (false );
124
+ if (is_string ($ orderAddress ->getRegion ())) {
125
+ /** @var RegionInterface $region */
126
+ $ region = $ this ->regionFactory ->create ();
127
+ $ region ->setRegion ($ orderAddress ->getRegion ());
128
+ $ region ->setRegionCode ($ orderAddress ->getRegionCode ());
129
+ $ region ->setRegionId ($ orderAddress ->getRegionId ());
130
+ $ customerAddress ->setRegion ($ region );
131
+ }
132
+
133
+ $ processedAddressData [] = $ addressData ;
134
+ $ customerAddresses [] = $ customerAddress ;
135
+ $ index = count ($ processedAddressData ) - 1 ;
127
136
}
128
137
129
- if ( is_string ( $ address -> getRegion ())) {
130
- /** @var RegionInterface $region */
131
- $ region = $ this -> regionFactory -> create ();
132
- $ region -> setRegion ( $ address -> getRegion () );
133
- $ region -> setRegionCode ( $ address -> getRegionCode ());
134
- $ region -> setRegionId ( $ address -> getRegionId ());
135
- $ customerAddress ->setRegion ( $ region );
138
+ $ customerAddress = $ customerAddresses [ $ index ];
139
+ // make sure that address type flags from equal addresses are stored in one resulted address
140
+ if ( $ orderAddress -> getAddressType () == OrderAddress:: TYPE_BILLING ) {
141
+ $ customerAddress -> setIsDefaultBilling ( true );
142
+ }
143
+ if ( $ orderAddress -> getAddressType () == OrderAddress:: TYPE_SHIPPING ) {
144
+ $ customerAddress ->setIsDefaultShipping ( true );
136
145
}
137
- $ customerData ['addresses ' ][] = $ customerAddress ;
138
146
}
139
147
148
+ $ customerData ['addresses ' ] = $ customerAddresses ;
149
+
140
150
return $ this ->customerFactory ->create (['data ' => $ customerData ]);
141
151
}
142
152
}
0 commit comments