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