From 2f7237a14f38520ac9f274c1f4349a546f152dc6 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Tue, 5 Feb 2019 21:19:05 -0500 Subject: [PATCH] Separate resolver for address shipping methods --- .../Cart/Address/AddressDataProvider.php | 10 +-- .../Address/ShippingMethodsDataProvider.php | 67 +++++++++++++++++++ .../Resolver/CartAddressShippingMethods.php | 46 +++++++++++++ .../Model/Resolver/CartAddresses.php | 7 +- .../Magento/QuoteGraphQl/etc/schema.graphqls | 2 +- 5 files changed, 117 insertions(+), 15 deletions(-) create mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingMethodsDataProvider.php create mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php index 64032cae64b5..73183ea903fc 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php @@ -49,10 +49,9 @@ public function __construct( * Collect and return information about shipping and billing addresses * * @param CartInterface $cart - * @param bool $includeShippingMethods * @return array */ - public function getCartAddresses(CartInterface $cart, $includeShippingMethods = false): array + public function getCartAddresses(CartInterface $cart): array { $addressData = []; $shippingAddress = $cart->getShippingAddress(); @@ -61,12 +60,6 @@ public function getCartAddresses(CartInterface $cart, $includeShippingMethods = if ($shippingAddress) { $shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class); $shippingData['address_type'] = 'SHIPPING'; - if ($includeShippingMethods) { - $shippingData['available_shipping_methods'] = $this->extractAvailableShippingRateData( - $cart, - $shippingAddress - ); - } $addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress)); } @@ -88,6 +81,7 @@ public function getCartAddresses(CartInterface $cart, $includeShippingMethods = private function extractAddressData(QuoteAddress $address): array { $addressData = [ + 'model' => $address, 'country' => [ 'code' => $address->getCountryId(), 'label' => $address->getCountry() diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingMethodsDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingMethodsDataProvider.php new file mode 100644 index 000000000000..06794d07877a --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingMethodsDataProvider.php @@ -0,0 +1,67 @@ +dataObjectConverter = $dataObjectConverter; + $this->shippingMethodConverter = $shippingMethodConverter; + } + + public function getAvailableShippingMethods(QuoteAddress $address): array + { + $methods = []; + + // Allow shipping rates by setting country id for new addresses + if (!$address->getCountryId() && $address->getCountryCode()) { + $address->setCountryId($address->getCountryCode()); + } + + $address->setCollectShippingRates(true); + $address->collectShippingRates(); + $cart = $address->getQuote(); + + $shippingRates = $address->getGroupedAllShippingRates(); + foreach ($shippingRates as $carrierRates) { + foreach ($carrierRates as $rate) { + $methods[] = $this->dataObjectConverter->toFlatArray( + $this->shippingMethodConverter->modelToDataObject($rate, $cart), + [], + ShippingMethodInterface::class + ); + } + } + + return ['available_shipping_methods' => $methods]; + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.php new file mode 100644 index 000000000000..e3c250237ad1 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.php @@ -0,0 +1,46 @@ +shippingMethodsDataProvider = $shippingMethodsDataProvider; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + if (!isset($value['model'])) { + throw new LocalizedException(__('"model" values should be specified')); + } + + return $this->shippingMethodsDataProvider->getAvailableShippingMethods($value['model']); + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php index 96305f9713c3..69544672bf12 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php @@ -43,11 +43,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $cart = $value['model']; - return $this->addressDataProvider->getCartAddresses($cart, $this->includeShippingMethods($info)); - } - - private function includeShippingMethods(ResolveInfo $info): bool - { - return $info->getFieldSelection()['available_shipping_methods'] ?? false; + return $this->addressDataProvider->getCartAddresses($cart); } } diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 86688a360ccf..c2c7412df4b6 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -112,7 +112,7 @@ type CartAddress { telephone: String address_type: AdressTypeEnum selected_shipping_method: CheckoutShippingMethod - available_shipping_methods: [CheckoutAvailableShippingMethod] + available_shipping_methods: [CheckoutAvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddressShippingMethods") items_weight: Float customer_notes: String cart_items: [CartItemQuantity]