Skip to content

Commit 42b2358

Browse files
committed
Consolidated cart address information provider
1 parent 97b519c commit 42b2358

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/Address/AddressDataProvider.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1111
use Magento\Quote\Api\Data\AddressInterface;
1212
use Magento\Quote\Api\Data\CartInterface;
13+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1314

1415
/**
1516
* Class AddressDataProvider
@@ -49,22 +50,45 @@ public function getCartAddresses(CartInterface $cart): array
4950
if ($shippingAddress) {
5051
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
5152
$shippingData['address_type'] = 'SHIPPING';
52-
$shippingData['selected_shipping_method'] = [
53-
'code' => $shippingAddress->getShippingMethod(),
54-
'label' => $shippingAddress->getShippingDescription(),
55-
'free_shipping' => $shippingAddress->getFreeShipping(),
56-
];
57-
$shippingData['items_weight'] = $shippingAddress->getWeight();
58-
$shippingData['customer_notes'] = $shippingAddress->getCustomerNotes();
59-
$addressData[] = $shippingData;
53+
$addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress));
6054
}
6155

6256
if ($billingAddress) {
6357
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
6458
$billingData['address_type'] = 'BILLING';
65-
$addressData[] = $billingData;
59+
$addressData[] = array_merge($billingData, $this->extractAddressData($billingAddress));
6660
}
6761

6862
return $addressData;
6963
}
64+
65+
/**
66+
* Extract the necessary address fields from address model
67+
*
68+
* @param QuoteAddress $address
69+
* @return array
70+
*/
71+
private function extractAddressData(QuoteAddress $address): array
72+
{
73+
$addressData = [
74+
'country' => [
75+
'code' => $address->getCountryId(),
76+
'label' => $address->getCountry()
77+
],
78+
'region' => [
79+
'code' => $address->getRegionCode(),
80+
'label' => $address->getRegion()
81+
],
82+
'street' => $address->getStreet(),
83+
'selected_shipping_method' => [
84+
'code' => $address->getShippingMethod(),
85+
'label' => $address->getShippingDescription(),
86+
'free_shipping' => $address->getFreeShipping(),
87+
],
88+
'items_weight' => $address->getWeight(),
89+
'customer_notes' => $address->getCustomerNotes()
90+
];
91+
92+
return $addressData;
93+
}
7094
}

0 commit comments

Comments
 (0)