8
8
namespace Magento \SalesGraphQl \Model \Order ;
9
9
10
10
use Magento \Quote \Model \Quote \Address ;
11
+ use Magento \Sales \Api \Data \OrderAddressInterface ;
11
12
use Magento \Sales \Api \Data \OrderInterface ;
12
- use Magento \Sales \Model \Order \Address as SalesOrderAddress ;
13
13
14
14
/**
15
15
* Class to fetch the order address details
@@ -20,19 +20,16 @@ class OrderAddress
20
20
* Get the order Shipping address
21
21
*
22
22
* @param OrderInterface $order
23
- * @return array
23
+ * @return array|null
24
24
*/
25
25
public function getOrderShippingAddress (
26
26
OrderInterface $ order
27
- ): array {
28
- $ shippingAddress = [];
29
- $ orderAddresses = $ order ->getAddresses ();
30
- foreach ($ orderAddresses as $ orderAddress ) {
31
- if ($ orderAddress ->getDataByKey ("address_type " ) === address::TYPE_SHIPPING ) {
32
- $ shippingAddress = $ this ->OrderAddressDataFormatter (
33
- $ orderAddress ,
34
- address::TYPE_SHIPPING
35
- );
27
+ ) {
28
+ $ shippingAddress = null ;
29
+ $ orderShippingAddress = $ order ->getShippingAddress () ?? null ;
30
+ if ($ orderShippingAddress ) {
31
+ if ($ orderShippingAddress ->getAddressType () === ADDRESS ::TYPE_SHIPPING ) {
32
+ $ shippingAddress = $ this ->OrderAddressDataFormatter ($ orderShippingAddress );
36
33
}
37
34
}
38
35
return $ shippingAddress ;
@@ -42,19 +39,16 @@ public function getOrderShippingAddress(
42
39
* Get the order billing address
43
40
*
44
41
* @param OrderInterface $order
45
- * @return array
42
+ * @return array|null
46
43
*/
47
44
public function getOrderBillingAddress (
48
45
OrderInterface $ order
49
- ): array {
50
- $ billingAddress = [];
51
- $ orderAddresses = $ order ->getAddresses ();
52
- foreach ($ orderAddresses as $ orderAddress ) {
53
- if ($ orderAddress ->getDataByKey ("address_type " ) === address::TYPE_BILLING ) {
54
- $ billingAddress = $ this ->OrderAddressDataFormatter (
55
- $ orderAddress ,
56
- address::TYPE_BILLING
57
- );
46
+ ) {
47
+ $ billingAddress = null ;
48
+ $ orderBillingAddress = $ order ->getBillingAddress () ?? null ;
49
+ if ($ orderBillingAddress ) {
50
+ if ($ orderBillingAddress ->getAddressType () === ADDRESS ::TYPE_BILLING ) {
51
+ $ billingAddress = $ this ->OrderAddressDataFormatter ($ orderBillingAddress );
58
52
}
59
53
}
60
54
return $ billingAddress ;
@@ -63,34 +57,29 @@ public function getOrderBillingAddress(
63
57
/**
64
58
* Customer Order address data formatter
65
59
*
66
- * @param SalesOrderAddress $orderAddress
67
- * @param string $addressType
60
+ * @param OrderAddressInterface $orderAddress
68
61
* @return array
69
62
*/
70
63
private function OrderAddressDataFormatter (
71
- SalesOrderAddress $ orderAddress ,
72
- string $ addressType
64
+ OrderAddressInterface $ orderAddress
73
65
): array {
74
- $ orderAddressData = [];
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 ' )),
89
- 'country_code ' => $ orderAddress ->getDataByKey ('country_id ' ),
90
- 'region ' => $ orderAddress ->getDataByKey ('region ' ),
91
- 'region_id ' => $ orderAddress ->getDataByKey ('region_id ' )
92
- ];
93
- }
94
- return $ orderAddressData ;
66
+ return
67
+ [
68
+ 'firstname ' => $ orderAddress ->getFirstname (),
69
+ 'lastname ' => $ orderAddress ->getLastname (),
70
+ 'middlename ' => $ orderAddress ->getMiddlename (),
71
+ 'postcode ' => $ orderAddress ->getPostcode (),
72
+ 'prefix ' => $ orderAddress ->getFirstname (),
73
+ 'suffix ' => $ orderAddress ->getFirstname (),
74
+ 'street ' => $ orderAddress ->getStreet (),
75
+ 'country_code ' => $ orderAddress ->getCountryId (),
76
+ 'city ' => $ orderAddress ->getCity (),
77
+ 'company ' => $ orderAddress ->getCompany (),
78
+ 'fax ' => $ orderAddress ->getFax (),
79
+ 'telephone ' => $ orderAddress ->getTelephone (),
80
+ 'vat_id ' => $ orderAddress ->getVatId (),
81
+ 'region_id ' => $ orderAddress ->getRegionId (),
82
+ 'region ' => $ orderAddress ->getRegion ()
83
+ ];
95
84
}
96
85
}
0 commit comments