|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\SalesGraphQl\Model\Order; |
| 9 | + |
| 10 | +use Magento\Customer\Api\Data\AddressInterface; |
| 11 | +use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress; |
| 12 | +use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData; |
| 13 | +use Magento\Sales\Api\Data\OrderInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class to get the order address details |
| 17 | + */ |
| 18 | +class OrderAddress |
| 19 | +{ |
| 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 | + /** |
| 43 | + * Get the order Shipping address |
| 44 | + * |
| 45 | + * @param OrderInterface $order |
| 46 | + * @param array $addressIds |
| 47 | + * @return array |
| 48 | + */ |
| 49 | + public function getShippingAddress( |
| 50 | + OrderInterface $order, |
| 51 | + array $addressIds |
| 52 | + ): array { |
| 53 | + $shippingAddress = []; |
| 54 | + $orderAddresses = $order->getAddresses(); |
| 55 | + 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 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return $shippingAddress; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get the order billing address |
| 75 | + * |
| 76 | + * @param OrderInterface $order |
| 77 | + * @param array $addressIds |
| 78 | + * @return array |
| 79 | + */ |
| 80 | + public function getBillingAddress( |
| 81 | + OrderInterface $order, |
| 82 | + array $addressIds |
| 83 | + ): array { |
| 84 | + $billingAddress = []; |
| 85 | + $orderAddresses = $order->getAddresses(); |
| 86 | + 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 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + return $billingAddress; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Customer Order address data formatter |
| 106 | + * |
| 107 | + * @param OrderInterface $order |
| 108 | + * @param string $addressType |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + private function curateCustomerOrderAddress( |
| 112 | + OrderInterface $order, |
| 113 | + string $addressType |
| 114 | + ): array { |
| 115 | + $orderAddressFields = []; |
| 116 | + $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')], |
| 124 | + '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' => [], |
| 133 | + ]; |
| 134 | + } |
| 135 | + } |
| 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 | + ]; |
| 168 | + } |
| 169 | +} |
0 commit comments