Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 6d6d5d5

Browse files
committed
Separate resolver for address shipping methods
1 parent 5b7b239 commit 6d6d5d5

File tree

5 files changed

+117
-15
lines changed

5 files changed

+117
-15
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ public function __construct(
4949
* Collect and return information about shipping and billing addresses
5050
*
5151
* @param CartInterface $cart
52-
* @param bool $includeShippingMethods
5352
* @return array
5453
*/
55-
public function getCartAddresses(CartInterface $cart, $includeShippingMethods = false): array
54+
public function getCartAddresses(CartInterface $cart): array
5655
{
5756
$addressData = [];
5857
$shippingAddress = $cart->getShippingAddress();
@@ -61,12 +60,6 @@ public function getCartAddresses(CartInterface $cart, $includeShippingMethods =
6160
if ($shippingAddress) {
6261
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
6362
$shippingData['address_type'] = 'SHIPPING';
64-
if ($includeShippingMethods) {
65-
$shippingData['available_shipping_methods'] = $this->extractAvailableShippingRateData(
66-
$cart,
67-
$shippingAddress
68-
);
69-
}
7063
$addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress));
7164
}
7265

@@ -88,6 +81,7 @@ public function getCartAddresses(CartInterface $cart, $includeShippingMethods =
8881
private function extractAddressData(QuoteAddress $address): array
8982
{
9083
$addressData = [
84+
'model' => $address,
9185
'country' => [
9286
'code' => $address->getCountryId(),
9387
'label' => $address->getCountry()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Cart\Address;
9+
10+
use Magento\Quote\Api\Data\ShippingMethodInterface;
11+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
12+
use Magento\Quote\Model\Cart\ShippingMethodConverter;
13+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
14+
15+
class ShippingMethodsDataProvider
16+
{
17+
/**
18+
* @var ExtensibleDataObjectConverter
19+
*/
20+
private $dataObjectConverter;
21+
22+
/**
23+
* @var ShippingMethodConverter
24+
*/
25+
private $shippingMethodConverter;
26+
27+
/**
28+
* AddressDataProvider constructor.
29+
*
30+
* @param ExtensibleDataObjectConverter $dataObjectConverter
31+
* @param ShippingMethodConverter $shippingMethodConverter
32+
*/
33+
public function __construct(
34+
ExtensibleDataObjectConverter $dataObjectConverter,
35+
ShippingMethodConverter $shippingMethodConverter
36+
) {
37+
$this->dataObjectConverter = $dataObjectConverter;
38+
$this->shippingMethodConverter = $shippingMethodConverter;
39+
}
40+
41+
public function getAvailableShippingMethods(QuoteAddress $address): array
42+
{
43+
$methods = [];
44+
45+
// Allow shipping rates by setting country id for new addresses
46+
if (!$address->getCountryId() && $address->getCountryCode()) {
47+
$address->setCountryId($address->getCountryCode());
48+
}
49+
50+
$address->setCollectShippingRates(true);
51+
$address->collectShippingRates();
52+
$cart = $address->getQuote();
53+
54+
$shippingRates = $address->getGroupedAllShippingRates();
55+
foreach ($shippingRates as $carrierRates) {
56+
foreach ($carrierRates as $rate) {
57+
$methods[] = $this->dataObjectConverter->toFlatArray(
58+
$this->shippingMethodConverter->modelToDataObject($rate, $cart->getQuoteCurrencyCode()),
59+
[],
60+
ShippingMethodInterface::class
61+
);
62+
}
63+
}
64+
65+
return $methods;
66+
}
67+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\ShippingMethodsDataProvider;
15+
16+
/**
17+
* @inheritdoc
18+
*/
19+
class CartAddressShippingMethods implements ResolverInterface
20+
{
21+
/**
22+
* @var ShippingMethodsDataProvider
23+
*/
24+
private $shippingMethodsDataProvider;
25+
26+
/**
27+
* @param ShippingMethodsDataProvider $shippingMethodsDataProvider
28+
*/
29+
public function __construct(
30+
ShippingMethodsDataProvider $shippingMethodsDataProvider
31+
) {
32+
$this->shippingMethodsDataProvider = $shippingMethodsDataProvider;
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" values should be specified'));
42+
}
43+
44+
return $this->shippingMethodsDataProvider->getAvailableShippingMethods($value['model']);
45+
}
46+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4343

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

46-
return $this->addressDataProvider->getCartAddresses($cart, $this->includeShippingMethods($info));
47-
}
48-
49-
private function includeShippingMethods(ResolveInfo $info): bool
50-
{
51-
return $info->getFieldSelection()['available_shipping_methods'] ?? false;
46+
return $this->addressDataProvider->getCartAddresses($cart);
5247
}
5348
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ type CartAddress {
112112
telephone: String
113113
address_type: AdressTypeEnum
114114
selected_shipping_method: CheckoutShippingMethod
115-
available_shipping_methods: [CheckoutAvailableShippingMethod]
115+
available_shipping_methods: [CheckoutAvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddressShippingMethods")
116116
items_weight: Float
117117
customer_notes: String
118118
cart_items: [CartItemQuantity]

0 commit comments

Comments
 (0)