7
7
8
8
namespace Magento \SalesGraphQl \Model \Order ;
9
9
10
- use Magento \Customer \Api \Data \AddressInterface ;
11
- use Magento \CustomerGraphQl \Model \Customer \Address \GetCustomerAddress ;
12
- use Magento \CustomerGraphQl \Model \Customer \ExtractCustomerData ;
10
+ use Magento \Quote \Model \Quote \Address ;
13
11
use Magento \Sales \Api \Data \OrderInterface ;
12
+ use Magento \Sales \Model \Order \Address as SalesOrderAddress ;
14
13
15
14
/**
16
- * Class to get the order address details
15
+ * Class to fetch the order address details
17
16
*/
18
17
class OrderAddress
19
18
{
20
- /**
21
- * @var GetCustomerAddress
22
- */
23
- private $ getCustomerAddress ;
24
-
25
- /**
26
- * @var ExtractCustomerData
27
- */
28
- private $ extractCustomerData ;
29
-
30
- /**
31
- * @param GetCustomerAddress $getCustomerAddress
32
- * @param ExtractCustomerData $extractCustomerData
33
- */
34
- public function __construct (
35
- GetCustomerAddress $ getCustomerAddress ,
36
- ExtractCustomerData $ extractCustomerData
37
- ) {
38
- $ this ->getCustomerAddress = $ getCustomerAddress ;
39
- $ this ->extractCustomerData = $ extractCustomerData ;
40
- }
41
-
42
19
/**
43
20
* Get the order Shipping address
44
21
*
45
22
* @param OrderInterface $order
46
- * @param array $addressIds
47
23
* @return array
48
24
*/
49
- public function getShippingAddress (
50
- OrderInterface $ order ,
51
- array $ addressIds
25
+ public function getOrderShippingAddress (
26
+ OrderInterface $ order
52
27
): array {
53
28
$ shippingAddress = [];
54
29
$ orderAddresses = $ order ->getAddresses ();
55
30
foreach ($ orderAddresses as $ orderAddress ) {
56
- $ addressType = $ orderAddress ->getDataByKey ("address_type " );
57
- if ($ addressType === 'shipping ' ) {
58
- $ customerAddressId = (int )$ orderAddress ->getDataByKey ('customer_address_id ' );
59
- if (in_array ($ customerAddressId , $ addressIds )) {
60
- $ customerData = $ this ->getCustomerAddress ->execute (
61
- $ customerAddressId ,
62
- (int )$ order ->getCustomerId ()
63
- );
64
- $ shippingAddress = $ this ->extractOrderAddress ($ customerData );
65
- } else {
66
- $ shippingAddress = $ this ->curateCustomerOrderAddress ($ order , $ addressType );
67
- }
31
+ if ($ orderAddress ->getDataByKey ("address_type " ) === address::TYPE_SHIPPING ) {
32
+ $ shippingAddress = $ this ->OrderAddressDataFormatter (
33
+ $ orderAddress ,
34
+ address::TYPE_SHIPPING
35
+ );
68
36
}
69
37
}
70
38
return $ shippingAddress ;
@@ -74,28 +42,19 @@ public function getShippingAddress(
74
42
* Get the order billing address
75
43
*
76
44
* @param OrderInterface $order
77
- * @param array $addressIds
78
45
* @return array
79
46
*/
80
- public function getBillingAddress (
81
- OrderInterface $ order ,
82
- array $ addressIds
47
+ public function getOrderBillingAddress (
48
+ OrderInterface $ order
83
49
): array {
84
50
$ billingAddress = [];
85
51
$ orderAddresses = $ order ->getAddresses ();
86
52
foreach ($ orderAddresses as $ orderAddress ) {
87
- $ addressType = $ orderAddress ->getDataByKey ("address_type " );
88
- if ($ addressType === 'billing ' ) {
89
- $ customerAddressId = (int )$ orderAddress ->getDataByKey ('customer_address_id ' );
90
- if (in_array ($ customerAddressId , $ addressIds )) {
91
- $ customerData = $ this ->getCustomerAddress ->execute (
92
- $ customerAddressId ,
93
- (int )$ order ->getCustomerId ()
94
- );
95
- $ billingAddress = $ this ->extractOrderAddress ($ customerData );
96
- } else {
97
- $ billingAddress = $ this ->curateCustomerOrderAddress ($ order , $ addressType );
98
- }
53
+ if ($ orderAddress ->getDataByKey ("address_type " ) === address::TYPE_BILLING ) {
54
+ $ billingAddress = $ this ->OrderAddressDataFormatter (
55
+ $ orderAddress ,
56
+ address::TYPE_BILLING
57
+ );
99
58
}
100
59
}
101
60
return $ billingAddress ;
@@ -104,66 +63,34 @@ public function getBillingAddress(
104
63
/**
105
64
* Customer Order address data formatter
106
65
*
107
- * @param OrderInterface $order
66
+ * @param SalesOrderAddress $orderAddress
108
67
* @param string $addressType
109
68
* @return array
110
69
*/
111
- private function curateCustomerOrderAddress (
112
- OrderInterface $ order ,
70
+ private function OrderAddressDataFormatter (
71
+ SalesOrderAddress $ orderAddress ,
113
72
string $ addressType
114
73
): array {
115
- $ orderAddressFields = [];
116
74
$ orderAddressData = [];
117
- $ orderAddresses = $ order ->getAddresses ();
118
- foreach ($ orderAddresses as $ orderAddress ) {
119
- if ($ addressType === $ orderAddress ->getDataByKey ("address_type " )) {
120
- $ orderAddressData = $ orderAddress ->getData ();
121
- $ orderAddressFields = [
122
- 'id ' => $ orderAddress ->getDataByKey ('entity_id ' ),
123
- 'street ' => [$ street = $ orderAddress ->getDataByKey ('street ' )],
75
+ if ($ addressType === $ orderAddress ->getDataByKey ("address_type " )) {
76
+ $ orderAddressData = [
77
+ 'firstname ' => $ orderAddress ->getDataByKey ('firstname ' ),
78
+ 'lastname ' => $ orderAddress ->getDataByKey ('lastname ' ),
79
+ 'middlename ' => $ orderAddress ->getDataByKey ('middlename ' ),
80
+ 'postcode ' => $ orderAddress ->getDataByKey ('postcode ' ),
81
+ 'prefix ' => $ orderAddress ->getDataByKey ('prefix ' ),
82
+ 'suffix ' => $ orderAddress ->getDataByKey ('suffix ' ),
83
+ 'city ' => $ orderAddress ->getDataByKey ('city ' ),
84
+ 'company ' => $ orderAddress ->getDataByKey ('company ' ),
85
+ 'fax ' => $ orderAddress ->getDataByKey ('fax ' ),
86
+ 'telephone ' => $ orderAddress ->getDataByKey ('telephone ' ),
87
+ 'vat_id ' => $ orderAddress ->getDataByKey ('vat_id ' ),
88
+ 'street ' => explode ("\n" , $ orderAddress ->getDataByKey ('street ' )),
124
89
'country_code ' => $ orderAddress ->getDataByKey ('country_id ' ),
125
- 'region ' => [
126
- 'region ' => $ orderAddress ->getDataByKey ('region ' ),
127
- 'region_id ' => $ orderAddress ->getDataByKey ('region_id ' ),
128
- 'region_code ' => $ orderAddress ->getDataByKey ('region ' )
129
- ],
130
- 'default_billing ' => 0 ,
131
- 'default_shipping ' => 0 ,
132
- 'extension_attributes ' => [],
90
+ 'region ' => $ orderAddress ->getDataByKey ('region ' ),
91
+ 'region_id ' => $ orderAddress ->getDataByKey ('region_id ' )
133
92
];
134
- }
135
93
}
136
- return array_merge ($ orderAddressData , $ orderAddressFields );
137
- }
138
-
139
- private function extractOrderAddress (AddressInterface $ customerData )
140
- {
141
- return [
142
- 'id ' => $ customerData ->getId (),
143
- 'firstname ' => $ customerData ->getFirstname (),
144
- 'lastname ' => $ customerData ->getLastname (),
145
- 'middlename ' => $ customerData ->getMiddlename (),
146
- 'postcode ' => $ customerData ->getPostcode (),
147
- 'prefix ' => $ customerData ->getFirstname (),
148
- 'suffix ' => $ customerData ->getFirstname (),
149
- 'street ' => $ customerData ->getStreet (),
150
- 'country_code ' => $ customerData ->getCountryId (),
151
- 'city ' => $ customerData ->getCity (),
152
- 'company ' => $ customerData ->getCompany (),
153
- 'fax ' => $ customerData ->getFax (),
154
- 'telephone ' => $ customerData ->getTelephone (),
155
- 'vat_id ' => $ customerData ->getVatId (),
156
- 'default_billing ' => $ customerData ->isDefaultBilling () ?? 0 ,
157
- 'default_shipping ' => $ customerData ->isDefaultShipping () ?? 0 ,
158
- 'region_id ' => $ customerData ->getRegion ()->getRegionId (),
159
- 'extension_attributes ' => [
160
- $ customerData ->getExtensionAttributes ()
161
- ],
162
- 'region ' => [
163
- 'region ' => $ customerData ->getRegion ()->getRegion (),
164
- 'region_id ' => $ customerData ->getRegion ()->getRegionId (),
165
- 'region_code ' => $ customerData ->getRegion ()->getRegionCode ()
166
- ],
167
- ];
94
+ return $ orderAddressData ;
168
95
}
169
96
}
0 commit comments