Skip to content

Commit e72936f

Browse files
committed
MC-35653:MyAccount :: Order Details :: Payments Methods, shipping address, billing address by Order Number
- Added code changes for addresses and payment method
1 parent fa6d291 commit e72936f

File tree

3 files changed

+167
-6
lines changed

3 files changed

+167
-6
lines changed

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\Framework\Api\SearchCriteriaBuilder;
1111
use Magento\Framework\Exception\InputException;
12-
use Magento\Framework\Exception\LocalizedException;
1312
use Magento\Framework\GraphQl\Config\Element\Field;
1413
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1514
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -18,6 +17,8 @@
1817
use Magento\Sales\Api\Data\OrderInterface;
1918
use Magento\Sales\Api\OrderRepositoryInterface;
2019
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderFilter;
20+
use Magento\SalesGraphQl\Model\SalesItem\ExtractOrderAddressDetails;
21+
use Magento\SalesGraphQl\Model\SalesItem\ExtractOrderPaymentDetails;
2122
use Magento\Store\Api\Data\StoreInterface;
2223

2324
/**
@@ -30,6 +31,16 @@ class CustomerOrders implements ResolverInterface
3031
*/
3132
private $searchCriteriaBuilder;
3233

34+
/**
35+
* @var ExtractOrderAddressDetails
36+
*/
37+
private $extractOrderAddressDetails;
38+
39+
/**
40+
* @var ExtractOrderPaymentDetails
41+
*/
42+
private $extractOrderPaymentDetails;
43+
3344
/**
3445
* @var OrderRepositoryInterface
3546
*/
@@ -42,15 +53,21 @@ class CustomerOrders implements ResolverInterface
4253

4354
/**
4455
* @param OrderRepositoryInterface $orderRepository
56+
* @param ExtractOrderAddressDetails $extractOrderAddressDetails
57+
* @param ExtractOrderPaymentDetails $extractOrderPaymentDetails
4558
* @param SearchCriteriaBuilder $searchCriteriaBuilder
4659
* @param OrderFilter $orderFilter
4760
*/
4861
public function __construct(
4962
OrderRepositoryInterface $orderRepository,
63+
ExtractOrderAddressDetails $extractOrderAddressDetails,
64+
ExtractOrderPaymentDetails $extractOrderPaymentDetails,
5065
SearchCriteriaBuilder $searchCriteriaBuilder,
5166
OrderFilter $orderFilter
5267
) {
5368
$this->orderRepository = $orderRepository;
69+
$this->extractOrderAddressDetails = $extractOrderAddressDetails;
70+
$this->extractOrderPaymentDetails = $extractOrderPaymentDetails;
5471
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
5572
$this->orderFilter = $orderFilter;
5673
}
@@ -77,19 +94,29 @@ public function resolve(
7794
$userId = $context->getUserId();
7895
/** @var StoreInterface $store */
7996
$store = $context->getExtensionAttributes()->getStore();
97+
$customerModel = $value['model'];
98+
$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+
}
106+
}
80107

81108
try {
82-
$searchResult = $this->getSearchResult($args, (int) $userId, (int)$store->getId());
109+
$searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId());
83110
$maxPages = (int)ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
84111
} catch (InputException $e) {
85112
throw new GraphQlInputException(__($e->getMessage()));
86113
}
87114

88115
return [
89116
'total_count' => $searchResult->getTotalCount(),
90-
'items' => $this->formatOrdersArray($searchResult->getItems()),
91-
'page_info' => [
92-
'page_size' => $searchResult->getPageSize(),
117+
'items' => $this->formatOrdersArray($searchResult->getItems(), $address),
118+
'page_info' => [
119+
'page_size' => $searchResult->getPageSize(),
93120
'current_page' => $searchResult->getCurPage(),
94121
'total_pages' => $maxPages,
95122
]
@@ -100,11 +127,13 @@ public function resolve(
100127
* Format order models for graphql schema
101128
*
102129
* @param OrderInterface[] $orderModels
130+
* @param array $address
103131
* @return array
104132
*/
105-
private function formatOrdersArray(array $orderModels)
133+
private function formatOrdersArray(array $orderModels, array $address)
106134
{
107135
$ordersArray = [];
136+
108137
foreach ($orderModels as $orderModel) {
109138
$ordersArray[] = [
110139
'created_at' => $orderModel->getCreatedAt(),
@@ -116,6 +145,9 @@ private function formatOrdersArray(array $orderModels)
116145
'order_number' => $orderModel->getIncrementId(),
117146
'status' => $orderModel->getStatusLabel(),
118147
'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),
119151
'model' => $orderModel,
120152
];
121153
}
@@ -144,3 +176,4 @@ private function getSearchResult(array $args, int $userId, int $storeId)
144176
return $this->orderRepository->getList($this->searchCriteriaBuilder->create());
145177
}
146178
}
179+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\SalesItem;
9+
10+
use Magento\Sales\Api\Data\OrderInterface;
11+
12+
/**
13+
* Class to extract the order address details
14+
*/
15+
class ExtractOrderAddressDetails
16+
{
17+
/**
18+
* Get Shipping address details
19+
*
20+
* @param OrderInterface $order
21+
* @return array
22+
*/
23+
public function getShippingAddressDetails(
24+
OrderInterface $order
25+
): array {
26+
$shippingAddressFields = [];
27+
$shippingAddressData = [];
28+
$orderAddresses = $order->getAddresses();
29+
foreach ($orderAddresses as $orderAddress) {
30+
$addressType = $orderAddress->getDataByKey("address_type");
31+
if ($addressType === 'shipping') {
32+
$shippingAddressData = $orderAddress->getData();
33+
$shippingAddressFields = [
34+
'id' => $orderAddress->getDataByKey('entity_id'),
35+
'street' => $orderAddress->getDataByKey('street'),
36+
'country_code' => $orderAddress->getDataByKey('country_id'),
37+
'region' => [
38+
'region' => $orderAddress->getDataByKey('region'),
39+
'region_id' => $orderAddress->getDataByKey('region_id'),
40+
'region_code' => $orderAddress->getDataByKey('region')
41+
],
42+
];
43+
}
44+
}
45+
return array_merge($shippingAddressData, $shippingAddressFields);
46+
}
47+
48+
/**
49+
* Get Billing address details
50+
*
51+
* @param OrderInterface $order
52+
* @return array
53+
*/
54+
public function getBillingAddressDetails(
55+
OrderInterface $order
56+
): array {
57+
$billingAddressFields = [];
58+
$billingAddressFieldsData = [];
59+
$orderAddresses = $order->getAddresses();
60+
foreach ($orderAddresses as $orderAddress) {
61+
$addressType = $orderAddress->getDataByKey("address_type");
62+
if ($addressType === 'billing') {
63+
$billingAddressFieldsData = $orderAddress->getData();
64+
$billingAddressFields = [
65+
'id' => $orderAddress->getDataByKey('entity_id'),
66+
'street' => $orderAddress->getDataByKey('street'),
67+
'country_code' => $orderAddress->getDataByKey('country_id'),
68+
'region' => [
69+
'region' => $orderAddress->getDataByKey('region'),
70+
'region_id' => $orderAddress->getDataByKey('region_id'),
71+
'region_code' => $orderAddress->getDataByKey('region')
72+
],
73+
];
74+
}
75+
}
76+
return array_merge($billingAddressFieldsData, $billingAddressFields);
77+
}
78+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\SalesItem;
9+
10+
use Magento\Sales\Api\Data\OrderInterface;
11+
12+
/**
13+
* Class to extract the order payment details
14+
*/
15+
class ExtractOrderPaymentDetails
16+
{
17+
/**
18+
* @param OrderInterface $orderModel
19+
* @return array
20+
*/
21+
public function getOrderPaymentMethodDetails(OrderInterface $orderModel): array
22+
{
23+
$orderPayment = $orderModel->getPayment();
24+
$additionalInformationCcType = $orderPayment->getCcType();
25+
$additionalInformationCcNumber = $orderPayment->getCcLast4();
26+
if ($orderPayment->getMethod() === 'checkmo' || $orderPayment->getMethod() === 'free') {
27+
$additionalData = [];
28+
} else {
29+
$additionalData = [
30+
[
31+
'name' => 'Credit Card Type',
32+
'value' => $additionalInformationCcType ?? null
33+
],
34+
[
35+
'name' => 'Credit Card Number',
36+
'value' => $additionalInformationCcNumber ?? null
37+
]
38+
];
39+
}
40+
41+
return [
42+
[
43+
'name' => $orderPayment->getAdditionalInformation()['method_title'],
44+
'type' => $orderPayment->getMethod(),
45+
'additional_data' => $additionalData
46+
]
47+
];
48+
}
49+
}
50+

0 commit comments

Comments
 (0)