Skip to content

Commit c88e975

Browse files
committed
Updated logic to match a new schema
1 parent 15c9377 commit c88e975

File tree

5 files changed

+93
-31
lines changed

5 files changed

+93
-31
lines changed

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,45 @@ public function __construct(
3636
}
3737

3838
/**
39-
* Collect and return information about shipping and billing addresses
39+
* Collect and return information about shipping addresses
4040
*
4141
* @param CartInterface $cart
4242
* @return array
4343
*/
44-
public function getCartAddresses(CartInterface $cart): array
44+
public function getShippingAddresses(CartInterface $cart): array
4545
{
4646
$addressData = [];
4747
$shippingAddress = $cart->getShippingAddress();
48-
$billingAddress = $cart->getBillingAddress();
4948

5049
if ($shippingAddress) {
5150
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
52-
$shippingData['address_type'] = 'SHIPPING';
53-
$addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress));
51+
$shippingMethodData = explode('_', $shippingAddress->getShippingMethod());
52+
$shippingData['selected_shipping_method'] = [
53+
'carrier_code' => $shippingMethodData[0],
54+
'method_code' => $shippingMethodData[1],
55+
'label' => $shippingAddress->getShippingDescription(),
56+
'free_shipping' => $shippingAddress->getFreeShipping()
57+
];
58+
$addressData['shipping_addresses'] = array_merge($shippingData, $this->extractAddressData($shippingAddress));
5459
}
5560

61+
return $addressData;
62+
}
63+
64+
/**
65+
* Collect and return information about billing address
66+
*
67+
* @param CartInterface $cart
68+
* @return array
69+
*/
70+
public function getBillingAddress(CartInterface $cart): array
71+
{
72+
$addressData = [];
73+
$billingAddress = $cart->getBillingAddress();
74+
5675
if ($billingAddress) {
5776
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
58-
$billingData['address_type'] = 'BILLING';
59-
$addressData[] = array_merge($billingData, $this->extractAddressData($billingAddress));
77+
$addressData['billing_address'] = array_merge($billingData, $this->extractAddressData($billingAddress));
6078
}
6179

6280
return $addressData;
@@ -81,11 +99,6 @@ private function extractAddressData(QuoteAddress $address): array
8199
'label' => $address->getRegion()
82100
],
83101
'street' => $address->getStreet(),
84-
'selected_shipping_method' => [
85-
'code' => $address->getShippingMethod(),
86-
'label' => $address->getShippingDescription(),
87-
'free_shipping' => $address->getFreeShipping(),
88-
],
89102
'items_weight' => $address->getWeight(),
90103
'customer_notes' => $address->getCustomerNotes()
91104
];

app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php renamed to app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @inheritdoc
1818
*/
19-
class CartAddresses implements ResolverInterface
19+
class BillingAddress implements ResolverInterface
2020
{
2121
/**
2222
* @var AddressDataProvider
@@ -43,6 +43,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4343

4444
$cart = $value['model'];
4545

46-
return $this->addressDataProvider->getCartAddresses($cart);
46+
return $this->addressDataProvider->getBillingAddress($cart);
4747
}
4848
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\QuoteGraphQl\Model\Cart\Address\AddressDataProvider;
15+
16+
/**
17+
* @inheritdoc
18+
*/
19+
class ShippingAddresses implements ResolverInterface
20+
{
21+
/**
22+
* @var AddressDataProvider
23+
*/
24+
private $addressDataProvider;
25+
26+
/**
27+
* @param AddressDataProvider $addressDataProvider
28+
*/
29+
public function __construct(
30+
AddressDataProvider $addressDataProvider
31+
) {
32+
$this->addressDataProvider = $addressDataProvider;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
39+
{
40+
if (!isset($value['model'])) {
41+
throw new LocalizedException(__('"model" value should be specified'));
42+
}
43+
44+
$cart = $value['model'];
45+
46+
return $this->addressDataProvider->getShippingAddresses($cart);
47+
}
48+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ type Cart {
101101
cart_id: String
102102
items: [CartItemInterface]
103103
applied_coupon: AppliedCoupon
104-
addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddresses")
104+
shipping_addresses: [CartAddress] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
105+
billing_address: CartAddress @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
105106
}
106107

107108
type CartAddress {
@@ -115,7 +116,6 @@ type CartAddress {
115116
postcode: String
116117
country: CartAddressCountry
117118
telephone: String
118-
address_type: AdressTypeEnum
119119
selected_shipping_method: CheckoutShippingMethod
120120
available_shipping_methods: [CheckoutShippingMethod]
121121
items_weight: Float
@@ -139,10 +139,10 @@ type CartAddressCountry {
139139
}
140140

141141
type CheckoutShippingMethod {
142-
code: String
142+
carrier_code: String
143+
method_code: String
143144
label: String
144145
free_shipping: Boolean!
145-
error_message: String
146146
# TODO: Add more complex structure for shipping rates
147147
}
148148

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingMethodOnCartTest.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ public function testSetShippingMethodOnCart()
7979
self::assertArrayHasKey('setShippingMethodsOnCart', $response);
8080
self::assertArrayHasKey('cart', $response['setShippingMethodsOnCart']);
8181
self::assertEquals($maskedQuoteId, $response['setShippingMethodsOnCart']['cart']['cart_id']);
82-
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['addresses'];
83-
self::assertCount(2, $addressesInformation);
82+
$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
83+
self::assertCount(1, $addressesInformation);
84+
self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $shippingCarrierCode);
8485
self::assertEquals(
85-
$addressesInformation[0]['selected_shipping_method']['code'],
86-
$shippingCarrierCode . '_' . $shippingMethodCode
87-
);
86+
$addressesInformation[0]['selected_shipping_method']['method_code'], $shippingMethodCode);
8887
}
8988

9089
/**
@@ -211,19 +210,21 @@ private function prepareMutationQuery(
211210
setShippingMethodsOnCart(input:
212211
{
213212
cart_id: "$maskedQuoteId",
214-
shipping_methods: [
215-
{
216-
shipping_method_code: "$shippingMethodCode"
217-
shipping_carrier_code: "$shippingCarrierCode"
218-
cart_address_id: $shippingAddressId
213+
shipping_addresses: [{
214+
cart_address_id: $shippingAddressId
215+
shipping_method: {
216+
method_code: "$shippingMethodCode"
217+
carrier_code: "$shippingCarrierCode"
219218
}
220-
]}) {
219+
}]
220+
}) {
221221
222222
cart {
223223
cart_id,
224-
addresses {
224+
shipping_addresses {
225225
selected_shipping_method {
226-
code
226+
carrier_code
227+
method_code
227228
label
228229
}
229230
}

0 commit comments

Comments
 (0)