Skip to content

Commit ee05088

Browse files
committed
MC-35653:MyAccount :: Order Details :: Payments Methods, shipping address, billing address by Order Number
- Added new chnages for order payments and addressess
1 parent e72936f commit ee05088

File tree

5 files changed

+195
-107
lines changed

5 files changed

+195
-107
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}

app/code/Magento/SalesGraphQl/Model/SalesItem/ExtractOrderPaymentDetails.php renamed to app/code/Magento/SalesGraphQl/Model/Order/OrderPayments.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\SalesGraphQl\Model\SalesItem;
8+
namespace Magento\SalesGraphQl\Model\Order;
99

1010
use Magento\Sales\Api\Data\OrderInterface;
1111

1212
/**
1313
* Class to extract the order payment details
1414
*/
15-
class ExtractOrderPaymentDetails
15+
class OrderPayments
1616
{
1717
/**
1818
* @param OrderInterface $orderModel
1919
* @return array
2020
*/
21-
public function getOrderPaymentMethodDetails(OrderInterface $orderModel): array
21+
public function getOrderPaymentMethod(OrderInterface $orderModel): array
2222
{
2323
$orderPayment = $orderModel->getPayment();
2424
$additionalInformationCcType = $orderPayment->getCcType();

app/code/Magento/SalesGraphQl/Model/Resolver/CustomerOrders.php

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1717
use Magento\Sales\Api\Data\OrderInterface;
1818
use Magento\Sales\Api\OrderRepositoryInterface;
19+
use Magento\SalesGraphQl\Model\Order\OrderAddress;
20+
use Magento\SalesGraphQl\Model\Order\OrderPayments;
1921
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderFilter;
20-
use Magento\SalesGraphQl\Model\SalesItem\ExtractOrderAddressDetails;
21-
use Magento\SalesGraphQl\Model\SalesItem\ExtractOrderPaymentDetails;
2222
use Magento\Store\Api\Data\StoreInterface;
2323

2424
/**
@@ -32,14 +32,14 @@ class CustomerOrders implements ResolverInterface
3232
private $searchCriteriaBuilder;
3333

3434
/**
35-
* @var ExtractOrderAddressDetails
35+
* @var OrderAddress
3636
*/
37-
private $extractOrderAddressDetails;
37+
private $orderAddress;
3838

3939
/**
40-
* @var ExtractOrderPaymentDetails
40+
* @var OrderPayments
4141
*/
42-
private $extractOrderPaymentDetails;
42+
private $orderPayments;
4343

4444
/**
4545
* @var OrderRepositoryInterface
@@ -53,21 +53,21 @@ class CustomerOrders implements ResolverInterface
5353

5454
/**
5555
* @param OrderRepositoryInterface $orderRepository
56-
* @param ExtractOrderAddressDetails $extractOrderAddressDetails
57-
* @param ExtractOrderPaymentDetails $extractOrderPaymentDetails
56+
* @param OrderAddress $orderAddress
57+
* @param OrderPayments $orderPayments
5858
* @param SearchCriteriaBuilder $searchCriteriaBuilder
5959
* @param OrderFilter $orderFilter
6060
*/
6161
public function __construct(
6262
OrderRepositoryInterface $orderRepository,
63-
ExtractOrderAddressDetails $extractOrderAddressDetails,
64-
ExtractOrderPaymentDetails $extractOrderPaymentDetails,
63+
OrderAddress $orderAddress,
64+
OrderPayments $orderPayments,
6565
SearchCriteriaBuilder $searchCriteriaBuilder,
6666
OrderFilter $orderFilter
6767
) {
6868
$this->orderRepository = $orderRepository;
69-
$this->extractOrderAddressDetails = $extractOrderAddressDetails;
70-
$this->extractOrderPaymentDetails = $extractOrderPaymentDetails;
69+
$this->orderAddress = $orderAddress;
70+
$this->orderPayments = $orderPayments;
7171
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
7272
$this->orderFilter = $orderFilter;
7373
}
@@ -96,13 +96,9 @@ public function resolve(
9696
$store = $context->getExtensionAttributes()->getStore();
9797
$customerModel = $value['model'];
9898
$address = $customerModel->getAddresses();
99-
$addressArrayData = [];
100-
foreach ($address as $key => $addressArray) {
101-
$addressArrayData[$key] = $addressArray;
102-
foreach ($addressArray as $addressData) {
103-
$shipping = $addressData->isDefaultshipping();
104-
$billing = $addressData->isDefaultBilling();
105-
}
99+
$addressIds = [];
100+
foreach ($address as $key => $addressData) {
101+
$addressIds[$key] = (int)$addressData->getId();
106102
}
107103

108104
try {
@@ -114,7 +110,7 @@ public function resolve(
114110

115111
return [
116112
'total_count' => $searchResult->getTotalCount(),
117-
'items' => $this->formatOrdersArray($searchResult->getItems(), $address),
113+
'items' => $this->formatOrdersArray($searchResult->getItems(), $addressIds),
118114
'page_info' => [
119115
'page_size' => $searchResult->getPageSize(),
120116
'current_page' => $searchResult->getCurPage(),
@@ -127,14 +123,15 @@ public function resolve(
127123
* Format order models for graphql schema
128124
*
129125
* @param OrderInterface[] $orderModels
130-
* @param array $address
126+
* @param array $addressIds
131127
* @return array
132128
*/
133-
private function formatOrdersArray(array $orderModels, array $address)
129+
private function formatOrdersArray(array $orderModels, array $addressIds)
134130
{
135131
$ordersArray = [];
136132

137133
foreach ($orderModels as $orderModel) {
134+
138135
$ordersArray[] = [
139136
'created_at' => $orderModel->getCreatedAt(),
140137
'grand_total' => $orderModel->getGrandTotal(),
@@ -145,9 +142,9 @@ private function formatOrdersArray(array $orderModels, array $address)
145142
'order_number' => $orderModel->getIncrementId(),
146143
'status' => $orderModel->getStatusLabel(),
147144
'shipping_method' => $orderModel->getShippingDescription(),
148-
'billing_address' => $this->extractOrderAddressDetails->getBillingAddressDetails($orderModel),
149-
'shipping_address' => $this->extractOrderAddressDetails->getShippingAddressDetails($orderModel),
150-
'payment_methods' => $this->extractOrderPaymentDetails->getOrderPaymentMethodDetails($orderModel),
145+
'shipping_address' => $this->orderAddress->getShippingAddress($orderModel, $addressIds),
146+
'billing_address' => $this->orderAddress->getBillingAddress($orderModel, $addressIds),
147+
'payment_methods' => $this->orderPayments->getOrderPaymentMethod($orderModel),
151148
'model' => $orderModel,
152149
];
153150
}
@@ -176,4 +173,3 @@ private function getSearchResult(array $args, int $userId, int $storeId)
176173
return $this->orderRepository->getList($this->searchCriteriaBuilder->create());
177174
}
178175
}
179-

app/code/Magento/SalesGraphQl/Model/SalesItem/ExtractOrderAddressDetails.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)