Skip to content

Commit 3afc1db

Browse files
committed
MC-35653:MyAccount :: Order Details :: Payments Methods, shipping address, billing address by Order Number
- Schema changes and related implementation changes are added
1 parent 9116f4d commit 3afc1db

File tree

3 files changed

+61
-124
lines changed

3 files changed

+61
-124
lines changed

app/code/Magento/SalesGraphQl/Model/Order/OrderAddress.php

Lines changed: 37 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,32 @@
77

88
namespace Magento\SalesGraphQl\Model\Order;
99

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;
1311
use Magento\Sales\Api\Data\OrderInterface;
12+
use Magento\Sales\Model\Order\Address as SalesOrderAddress;
1413

1514
/**
16-
* Class to get the order address details
15+
* Class to fetch the order address details
1716
*/
1817
class OrderAddress
1918
{
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-
4219
/**
4320
* Get the order Shipping address
4421
*
4522
* @param OrderInterface $order
46-
* @param array $addressIds
4723
* @return array
4824
*/
49-
public function getShippingAddress(
50-
OrderInterface $order,
51-
array $addressIds
25+
public function getOrderShippingAddress(
26+
OrderInterface $order
5227
): array {
5328
$shippingAddress = [];
5429
$orderAddresses = $order->getAddresses();
5530
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+
);
6836
}
6937
}
7038
return $shippingAddress;
@@ -74,28 +42,19 @@ public function getShippingAddress(
7442
* Get the order billing address
7543
*
7644
* @param OrderInterface $order
77-
* @param array $addressIds
7845
* @return array
7946
*/
80-
public function getBillingAddress(
81-
OrderInterface $order,
82-
array $addressIds
47+
public function getOrderBillingAddress(
48+
OrderInterface $order
8349
): array {
8450
$billingAddress = [];
8551
$orderAddresses = $order->getAddresses();
8652
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+
);
9958
}
10059
}
10160
return $billingAddress;
@@ -104,66 +63,34 @@ public function getBillingAddress(
10463
/**
10564
* Customer Order address data formatter
10665
*
107-
* @param OrderInterface $order
66+
* @param SalesOrderAddress $orderAddress
10867
* @param string $addressType
10968
* @return array
11069
*/
111-
private function curateCustomerOrderAddress(
112-
OrderInterface $order,
70+
private function OrderAddressDataFormatter(
71+
SalesOrderAddress $orderAddress,
11372
string $addressType
11473
): array {
115-
$orderAddressFields = [];
11674
$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')),
12489
'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')
13392
];
134-
}
13593
}
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;
16895
}
16996
}

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,6 @@ public function resolve(
9494
$userId = $context->getUserId();
9595
/** @var StoreInterface $store */
9696
$store = $context->getExtensionAttributes()->getStore();
97-
$customerModel = $value['model'];
98-
$address = $customerModel->getAddresses();
99-
$addressIds = [];
100-
foreach ($address as $key => $addressData) {
101-
$addressIds[$key] = (int)$addressData->getId();
102-
}
103-
10497
try {
10598
$searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId());
10699
$maxPages = (int)ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
@@ -110,7 +103,7 @@ public function resolve(
110103

111104
return [
112105
'total_count' => $searchResult->getTotalCount(),
113-
'items' => $this->formatOrdersArray($searchResult->getItems(), $addressIds),
106+
'items' => $this->formatOrdersArray($searchResult->getItems()),
114107
'page_info' => [
115108
'page_size' => $searchResult->getPageSize(),
116109
'current_page' => $searchResult->getCurPage(),
@@ -123,10 +116,9 @@ public function resolve(
123116
* Format order models for graphql schema
124117
*
125118
* @param OrderInterface[] $orderModels
126-
* @param array $addressIds
127119
* @return array
128120
*/
129-
private function formatOrdersArray(array $orderModels, array $addressIds)
121+
private function formatOrdersArray(array $orderModels)
130122
{
131123
$ordersArray = [];
132124

@@ -142,8 +134,8 @@ private function formatOrdersArray(array $orderModels, array $addressIds)
142134
'order_number' => $orderModel->getIncrementId(),
143135
'status' => $orderModel->getStatusLabel(),
144136
'shipping_method' => $orderModel->getShippingDescription(),
145-
'shipping_address' => $this->orderAddress->getShippingAddress($orderModel, $addressIds),
146-
'billing_address' => $this->orderAddress->getBillingAddress($orderModel, $addressIds),
137+
'shipping_address' => $this->orderAddress->getOrderShippingAddress($orderModel),
138+
'billing_address' => $this->orderAddress->getOrderBillingAddress($orderModel),
147139
'payment_methods' => $this->orderPayments->getOrderPaymentMethod($orderModel),
148140
'model' => $orderModel,
149141
];

app/code/Magento/SalesGraphQl/etc/schema.graphqls

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ type CustomerOrder @doc(description: "Contains details about each of the custome
4848
invoices: [Invoice]! @doc(description: "A list of invoices for the order") @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\Invoices")
4949
shipments: [OrderShipment] @doc(description: "A list of shipments for the order")
5050
payment_methods: [PaymentMethod] @doc(description: "Payment details for the order")
51-
shipping_address: CustomerAddress @doc(description: "The shipping address for the order")
52-
billing_address: CustomerAddress @doc(description: "The billing address for the order")
51+
shipping_address: OrderAddress @doc(description: "The shipping address for the order")
52+
billing_address: OrderAddress @doc(description: "The billing address for the order")
5353
carrier: String @doc(description: "The shipping carrier for the order delivery")
5454
shipping_method: String @doc(description: "The delivery method for the order")
5555
comments: [CommentItem] @doc(description: "Comments about the order")
@@ -59,6 +59,24 @@ type CustomerOrder @doc(description: "Contains details about each of the custome
5959
grand_total: Float @deprecated(reason: "Use the totals.grand_total attribute instead")
6060
}
6161

62+
type OrderAddress @doc(description: "OrderAddress contains detailed information about the order billing and shipping addresses"){
63+
firstname: String! @doc(description: "The first name of the person associated with the shipping/billing address on the order")
64+
lastname: String! @doc(description: "The family name of the person associated with the shipping/billing address on the order")
65+
middlename: String @doc(description: "The middle name of the person associated with the shipping/billing address on the order")
66+
region: String @doc(description: "The state or province name on the order")
67+
region_id: ID @doc(description: "The unique ID for a pre-defined region on the order")
68+
country_code: CountryCodeEnum @doc(description: "The customer's country on the order")
69+
street: [String]! @doc(description: "An array of strings that define the street number and name on the order")
70+
company: String @doc(description: "The customer's company on the order")
71+
telephone: String! @doc(description: "The telephone number on the order")
72+
fax: String @doc(description: "The fax number on the order")
73+
postcode: String @doc(description: "The customer's order ZIP or postal code on the order")
74+
city: String! @doc(description: "The city or town on the order")
75+
prefix: String @doc(description: "An honorific, such as Dr., Mr., or Mrs. on the order")
76+
suffix: String @doc(description: "A value such as Sr., Jr., or III on the order")
77+
vat_id: String @doc(description: "The customer's Value-added tax (VAT) number (for corporate customers) on the order")
78+
}
79+
6280
interface OrderItemInterface @doc(description: "Order item details") @typeResolver(class: "Magento\\SalesGraphQl\\Model\\OrderItemTypeResolver") {
6381
id: ID! @doc(description: "The unique identifier of the order item")
6482
product_name: String @doc(description: "The name of the base product")

0 commit comments

Comments
 (0)