From 5b7b239e2797efce2c354452bd79499c1d8d0ff7 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Sun, 3 Feb 2019 16:34:04 -0500 Subject: [PATCH 1/5] Provide available shipping rates for addresses --- .../Cart/Address/AddressDataProvider.php | 54 ++++++++++++++- .../Model/Resolver/CartAddresses.php | 7 +- .../Magento/QuoteGraphQl/etc/schema.graphqls | 20 +++++- .../Quote/SetShippingAddressOnCartTest.php | 65 +++++++++++++++++++ 4 files changed, 140 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php index fb742477ec99..64032cae64b5 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php @@ -10,7 +10,9 @@ use Magento\Framework\Api\ExtensibleDataObjectConverter; use Magento\Quote\Api\Data\AddressInterface; use Magento\Quote\Api\Data\CartInterface; +use Magento\Quote\Api\Data\ShippingMethodInterface; use Magento\Quote\Model\Quote\Address as QuoteAddress; +use Magento\Quote\Model\Cart\ShippingMethodConverter; /** * Class AddressDataProvider @@ -24,24 +26,33 @@ class AddressDataProvider */ private $dataObjectConverter; + /** + * @var ShippingMethodConverter + */ + private $shippingMethodConverter; + /** * AddressDataProvider constructor. * * @param ExtensibleDataObjectConverter $dataObjectConverter + * @param ShippingMethodConverter $shippingMethodConverter */ public function __construct( - ExtensibleDataObjectConverter $dataObjectConverter + ExtensibleDataObjectConverter $dataObjectConverter, + ShippingMethodConverter $shippingMethodConverter ) { $this->dataObjectConverter = $dataObjectConverter; + $this->shippingMethodConverter = $shippingMethodConverter; } /** * Collect and return information about shipping and billing addresses * * @param CartInterface $cart + * @param bool $includeShippingMethods * @return array */ - public function getCartAddresses(CartInterface $cart): array + public function getCartAddresses(CartInterface $cart, $includeShippingMethods = false): array { $addressData = []; $shippingAddress = $cart->getShippingAddress(); @@ -50,6 +61,12 @@ public function getCartAddresses(CartInterface $cart): array 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)); } @@ -84,11 +101,42 @@ private function extractAddressData(QuoteAddress $address): array 'code' => $address->getShippingMethod(), 'label' => $address->getShippingDescription(), 'free_shipping' => $address->getFreeShipping(), + 'amount' => $address->getShippingAmount(), + 'base_amount' => $address->getBaseShippingAmount(), + 'amount_incl_tax' => $address->getShippingInclTax(), + 'base_amount_incl_tax' => $address->getBaseShippingInclTax(), ], 'items_weight' => $address->getWeight(), - 'customer_notes' => $address->getCustomerNotes() + 'customer_notes' => $address->getCustomerNotes(), + 'quote_id' => $address->getQuoteId(), ]; return $addressData; } + + private function extractAvailableShippingRateData(CartInterface $cart, QuoteAddress $address): array + { + $output = []; + + // Allow shipping rates by setting country id for new addresses + if (!$address->getCountryId() && $address->getCountryCode()) { + $address->setCountryId($address->getCountryCode()); + } + + $address->setCollectShippingRates(true); + $address->collectShippingRates(); + + $shippingRates = $address->getGroupedAllShippingRates(); + foreach ($shippingRates as $carrierRates) { + foreach ($carrierRates as $rate) { + $output[] = $this->dataObjectConverter->toFlatArray( + $this->shippingMethodConverter->modelToDataObject($rate, $cart->getQuoteCurrencyCode()), + [], + ShippingMethodInterface::class + ); + } + } + + return $output; + } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php index 69544672bf12..96305f9713c3 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php @@ -43,6 +43,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $cart = $value['model']; - return $this->addressDataProvider->getCartAddresses($cart); + return $this->addressDataProvider->getCartAddresses($cart, $this->includeShippingMethods($info)); + } + + private function includeShippingMethods(ResolveInfo $info): bool + { + return $info->getFieldSelection()['available_shipping_methods'] ?? false; } } diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 4c1101a5f90a..86688a360ccf 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: [CheckoutShippingMethod] + available_shipping_methods: [CheckoutAvailableShippingMethod] items_weight: Float customer_notes: String cart_items: [CartItemQuantity] @@ -138,7 +138,23 @@ type CheckoutShippingMethod { label: String free_shipping: Boolean! error_message: String - # TODO: Add more complex structure for shipping rates + amount: Float! + base_amount: Float! + amount_incl_tax: Float! + base_amount_incl_tax: Float! +} + +type CheckoutAvailableShippingMethod { + carrier_code: String! + carrier_title: String! + method_code: String! + method_title: String! + error_message: String + amount: Float! + base_amount: Float! + price_excl_tax: Float! + price_incl_tax: Float! + available: Boolean! } enum AdressTypeEnum { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php index a023d37895c2..9fcaa894425c 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php @@ -87,6 +87,18 @@ public function testSetNewGuestShippingAddressOnCart() city postcode telephone + available_shipping_methods { + amount + available + base_amount + carrier_code + carrier_title + error_message + method_code + method_title + price_excl_tax + price_incl_tax + } } } } @@ -99,6 +111,7 @@ public function testSetNewGuestShippingAddressOnCart() self::assertArrayHasKey('addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['addresses']); $this->assertNewShippingAddressFields($shippingAddressResponse); + $this->assertAvailableShippingRates($shippingAddressResponse); } /** @@ -340,6 +353,18 @@ public function testSetNewRegisteredCustomerShippingAddressOnCart() city postcode telephone + available_shipping_methods { + amount + available + base_amount + carrier_code + carrier_title + error_message + method_code + method_title + price_excl_tax + price_incl_tax + } } } } @@ -352,6 +377,7 @@ public function testSetNewRegisteredCustomerShippingAddressOnCart() self::assertArrayHasKey('addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['addresses']); $this->assertNewShippingAddressFields($shippingAddressResponse); + $this->assertAvailableShippingRates($shippingAddressResponse); } /** @@ -398,6 +424,18 @@ public function testSetSavedRegisteredCustomerShippingAddressOnCart() city postcode telephone + available_shipping_methods { + amount + available + base_amount + carrier_code + carrier_title + error_message + method_code + method_title + price_excl_tax + price_incl_tax + } } } } @@ -410,6 +448,7 @@ public function testSetSavedRegisteredCustomerShippingAddressOnCart() self::assertArrayHasKey('addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['addresses']); $this->assertSavedShippingAddressFields($shippingAddressResponse); + $this->assertAvailableShippingRates($shippingAddressResponse); } /** @@ -452,6 +491,32 @@ private function assertSavedShippingAddressFields(array $shippingAddressResponse $this->assertResponseFields($shippingAddressResponse, $assertionMap); } + /** + * Verify the expected shipping method is available + * + * @param array $shippingAddressResponse + */ + private function assertAvailableShippingRates(array $shippingAddressResponse): void + { + $this->assertArrayHasKey('available_shipping_methods', $shippingAddressResponse); + $rate = current($shippingAddressResponse['available_shipping_methods']); + + $assertionMap = [ + ['response_field' => 'amount', 'expected_value' => 5], + ['response_field' => 'available', 'expected_value' => true], + ['response_field' => 'base_amount', 'expected_value' => 5], + ['response_field' => 'carrier_code', 'expected_value' => 'flatrate'], + ['response_field' => 'carrier_title', 'expected_value' => 'Flat Rate'], + ['response_field' => 'error_message', 'expected_value' => ''], + ['response_field' => 'method_code', 'expected_value' => 'flatrate'], + ['response_field' => 'method_title', 'expected_value' => 'Fixed'], + ['response_field' => 'price_incl_tax', 'expected_value' => 5], + ['response_field' => 'price_excl_tax', 'expected_value' => 5], + ]; + + $this->assertResponseFields($rate, $assertionMap); + } + /** * @param string $username * @return array From 6d6d5d56282fd7851e1eb9af15cf9a58c308bda5 Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Tue, 5 Feb 2019 21:19:05 -0500 Subject: [PATCH 2/5] 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..fd9540194b42 --- /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->getQuoteCurrencyCode()), + [], + ShippingMethodInterface::class + ); + } + } + + return $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] From 2fa4f1f207b182e9c64913007b52d64333dadc82 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Thu, 14 Feb 2019 14:50:28 -0600 Subject: [PATCH 3/5] GraphQL-285: [Shipping methods] Get list of available shipping methods for current cart --- .../Cart/Address/AddressDataProvider.php | 136 ------------------ .../Address/BillingAddressDataProvider.php | 72 ---------- .../Address/ShippingAddressesDataProvider.php | 76 ---------- ...Address.php => ExtractDataFromAddress.php} | 32 +++-- .../Model/Resolver/BillingAddress.php | 24 ++-- .../Resolver/CartAddressShippingMethods.php | 46 ------ .../Model/Resolver/CartAddresses.php | 48 ------- .../Model/Resolver/ShippingAddresses.php | 26 ++-- .../AvailableShippingMethods.php} | 28 ++-- .../Magento/QuoteGraphQl/etc/schema.graphqls | 11 +- .../Quote/GetAvailableShippingMethodsTest.php | 123 ++++++++++++++++ 11 files changed, 201 insertions(+), 421 deletions(-) delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Address/BillingAddressDataProvider.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingAddressesDataProvider.php rename app/code/Magento/QuoteGraphQl/Model/Cart/{Address/Mapper/Address.php => ExtractDataFromAddress.php} (64%) delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.php delete mode 100644 app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php rename app/code/Magento/QuoteGraphQl/Model/{Cart/Address/ShippingMethodsDataProvider.php => Resolver/ShippingAdress/AvailableShippingMethods.php} (72%) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailableShippingMethodsTest.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php deleted file mode 100644 index 73183ea903fc..000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php +++ /dev/null @@ -1,136 +0,0 @@ -dataObjectConverter = $dataObjectConverter; - $this->shippingMethodConverter = $shippingMethodConverter; - } - - /** - * Collect and return information about shipping and billing addresses - * - * @param CartInterface $cart - * @return array - */ - public function getCartAddresses(CartInterface $cart): array - { - $addressData = []; - $shippingAddress = $cart->getShippingAddress(); - $billingAddress = $cart->getBillingAddress(); - - if ($shippingAddress) { - $shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class); - $shippingData['address_type'] = 'SHIPPING'; - $addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress)); - } - - if ($billingAddress) { - $billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class); - $billingData['address_type'] = 'BILLING'; - $addressData[] = array_merge($billingData, $this->extractAddressData($billingAddress)); - } - - return $addressData; - } - - /** - * Extract the necessary address fields from address model - * - * @param QuoteAddress $address - * @return array - */ - private function extractAddressData(QuoteAddress $address): array - { - $addressData = [ - 'model' => $address, - 'country' => [ - 'code' => $address->getCountryId(), - 'label' => $address->getCountry() - ], - 'region' => [ - 'code' => $address->getRegionCode(), - 'label' => $address->getRegion() - ], - 'street' => $address->getStreet(), - 'selected_shipping_method' => [ - 'code' => $address->getShippingMethod(), - 'label' => $address->getShippingDescription(), - 'free_shipping' => $address->getFreeShipping(), - 'amount' => $address->getShippingAmount(), - 'base_amount' => $address->getBaseShippingAmount(), - 'amount_incl_tax' => $address->getShippingInclTax(), - 'base_amount_incl_tax' => $address->getBaseShippingInclTax(), - ], - 'items_weight' => $address->getWeight(), - 'customer_notes' => $address->getCustomerNotes(), - 'quote_id' => $address->getQuoteId(), - ]; - - return $addressData; - } - - private function extractAvailableShippingRateData(CartInterface $cart, QuoteAddress $address): array - { - $output = []; - - // Allow shipping rates by setting country id for new addresses - if (!$address->getCountryId() && $address->getCountryCode()) { - $address->setCountryId($address->getCountryCode()); - } - - $address->setCollectShippingRates(true); - $address->collectShippingRates(); - - $shippingRates = $address->getGroupedAllShippingRates(); - foreach ($shippingRates as $carrierRates) { - foreach ($carrierRates as $rate) { - $output[] = $this->dataObjectConverter->toFlatArray( - $this->shippingMethodConverter->modelToDataObject($rate, $cart->getQuoteCurrencyCode()), - [], - ShippingMethodInterface::class - ); - } - } - - return $output; - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/BillingAddressDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/BillingAddressDataProvider.php deleted file mode 100644 index bcd781025a6d..000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/BillingAddressDataProvider.php +++ /dev/null @@ -1,72 +0,0 @@ -cartRepository = $cartRepository; - $this->addressMapper = $addressMapper; - $this->dataObjectConverter = $dataObjectConverter; - } - - /** - * Collect and return information about a billing addresses - * - * @param CartInterface $cart - * @return null|array - */ - public function getCartAddresses(CartInterface $cart): ?array - { - $cart = $this->cartRepository->get($cart->getId()); - $billingAddress = $cart->getBillingAddress(); - - if (!$billingAddress) { - return null; - } - $billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class); - $addressData = array_merge($billingData, $this->addressMapper->toNestedArray($billingAddress)); - - return $addressData; - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingAddressesDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingAddressesDataProvider.php deleted file mode 100644 index eb9d76080959..000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingAddressesDataProvider.php +++ /dev/null @@ -1,76 +0,0 @@ -dataObjectConverter = $dataObjectConverter; - $this->cartRepository = $cartRepository; - $this->addressMapper = $addressMapper; - } - - /** - * Collect and return information about shipping addresses - * - * @param CartInterface $cart - * @return array - */ - public function getCartAddresses(CartInterface $cart): array - { - $cart = $this->cartRepository->get($cart->getId()); - $addressData = []; - $shippingAddresses = $cart->getAllShippingAddresses(); - - if ($shippingAddresses) { - foreach ($shippingAddresses as $shippingAddress) { - $shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class); - $addressData[] = array_merge($shippingData, $this->addressMapper->toNestedArray($shippingAddress)); - } - } - - return $addressData; - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/Mapper/Address.php b/app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php similarity index 64% rename from app/code/Magento/QuoteGraphQl/Model/Cart/Address/Mapper/Address.php rename to app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php index 64ac407e2692..b0e5070315d8 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/Mapper/Address.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php @@ -5,26 +5,42 @@ */ declare(strict_types=1); -namespace Magento\QuoteGraphQl\Model\Cart\Address\Mapper; +namespace Magento\QuoteGraphQl\Model\Cart; +use Magento\Framework\Api\ExtensibleDataObjectConverter; +use Magento\Quote\Api\Data\AddressInterface; use Magento\Quote\Model\Quote\Address as QuoteAddress; /** - * Class Address - * * Extract the necessary address fields from an Address model */ -class Address +class ExtractDataFromAddress { /** - * Converts Address model data to nested array + * @var ExtensibleDataObjectConverter + */ + private $dataObjectConverter; + + /** + * @param ExtensibleDataObjectConverter $dataObjectConverter + */ + public function __construct(ExtensibleDataObjectConverter $dataObjectConverter) + { + $this->dataObjectConverter = $dataObjectConverter; + } + + /** + * Converts Address model to flat array * * @param QuoteAddress $address * @return array */ - public function toNestedArray(QuoteAddress $address): array + public function execute(QuoteAddress $address): array { - $addressData = [ + $addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class); + $addressData['model'] = $address; + + $addressData = array_merge($addressData, [ 'country' => [ 'code' => $address->getCountryId(), 'label' => $address->getCountry() @@ -41,7 +57,7 @@ public function toNestedArray(QuoteAddress $address): array ], 'items_weight' => $address->getWeight(), 'customer_notes' => $address->getCustomerNotes() - ]; + ]); if (!$address->hasItems()) { return $addressData; diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php index 7de5a4ac05c2..a03533ecefff 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php @@ -11,7 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\QuoteGraphQl\Model\Cart\Address\BillingAddressDataProvider; +use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromAddress; /** * @inheritdoc @@ -19,17 +19,16 @@ class BillingAddress implements ResolverInterface { /** - * @var BillingAddressDataProvider + * @var ExtractDataFromAddress */ - private $addressDataProvider; + private $extractDataFromAddress; /** - * @param BillingAddressDataProvider $addressDataProvider + * @param ExtractDataFromAddress $extractDataFromAddress */ - public function __construct( - BillingAddressDataProvider $addressDataProvider - ) { - $this->addressDataProvider = $addressDataProvider; + public function __construct(ExtractDataFromAddress $extractDataFromAddress) + { + $this->extractDataFromAddress = $extractDataFromAddress; } /** @@ -40,9 +39,14 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value if (!isset($value['model'])) { throw new LocalizedException(__('"model" value should be specified')); } - $cart = $value['model']; - return $this->addressDataProvider->getCartAddresses($cart); + $billingAddress = $cart->getBillingAddress(); + if (null === $billingAddress) { + return null; + } + + $addressData = $this->extractDataFromAddress->execute($billingAddress); + return $addressData; } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.php deleted file mode 100644 index e3c250237ad1..000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddressShippingMethods.php +++ /dev/null @@ -1,46 +0,0 @@ -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 deleted file mode 100644 index 69544672bf12..000000000000 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php +++ /dev/null @@ -1,48 +0,0 @@ -addressDataProvider = $addressDataProvider; - } - - /** - * @inheritdoc - */ - public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) - { - if (!isset($value['model'])) { - throw new LocalizedException(__('"model" value should be specified')); - } - - $cart = $value['model']; - - return $this->addressDataProvider->getCartAddresses($cart); - } -} diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddresses.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddresses.php index caa0eee22d70..3a55ef9ae25a 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddresses.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddresses.php @@ -11,7 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; -use Magento\QuoteGraphQl\Model\Cart\Address\ShippingAddressesDataProvider; +use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromAddress; /** * @inheritdoc @@ -19,17 +19,16 @@ class ShippingAddresses implements ResolverInterface { /** - * @var ShippingAddressesDataProvider + * @var ExtractDataFromAddress */ - private $addressDataProvider; + private $extractDataFromAddress; /** - * @param ShippingAddressesDataProvider $addressDataProvider + * @param ExtractDataFromAddress $extractDataFromAddress */ - public function __construct( - ShippingAddressesDataProvider $addressDataProvider - ) { - $this->addressDataProvider = $addressDataProvider; + public function __construct(ExtractDataFromAddress $extractDataFromAddress) + { + $this->extractDataFromAddress = $extractDataFromAddress; } /** @@ -40,9 +39,16 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value if (!isset($value['model'])) { throw new LocalizedException(__('"model" value should be specified')); } - $cart = $value['model']; - return $this->addressDataProvider->getCartAddresses($cart); + $addressesData = []; + $shippingAddresses = $cart->getAllShippingAddresses(); + + if (count($shippingAddresses)) { + foreach ($shippingAddresses as $shippingAddress) { + $addressesData[] = $this->extractDataFromAddress->execute($shippingAddress); + } + } + return $addressesData; } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingMethodsDataProvider.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAdress/AvailableShippingMethods.php similarity index 72% rename from app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingMethodsDataProvider.php rename to app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAdress/AvailableShippingMethods.php index fd9540194b42..7804b8defe37 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingMethodsDataProvider.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAdress/AvailableShippingMethods.php @@ -5,14 +5,20 @@ */ declare(strict_types=1); -namespace Magento\QuoteGraphQl\Model\Cart\Address; +namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAdress; -use Magento\Quote\Api\Data\ShippingMethodInterface; use Magento\Framework\Api\ExtensibleDataObjectConverter; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Query\ResolverInterface; +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; +use Magento\Quote\Api\Data\ShippingMethodInterface; use Magento\Quote\Model\Cart\ShippingMethodConverter; -use Magento\Quote\Model\Quote\Address as QuoteAddress; -class ShippingMethodsDataProvider +/** + * @inheritdoc + */ +class AvailableShippingMethods implements ResolverInterface { /** * @var ExtensibleDataObjectConverter @@ -25,8 +31,6 @@ class ShippingMethodsDataProvider private $shippingMethodConverter; /** - * AddressDataProvider constructor. - * * @param ExtensibleDataObjectConverter $dataObjectConverter * @param ShippingMethodConverter $shippingMethodConverter */ @@ -38,9 +42,15 @@ public function __construct( $this->shippingMethodConverter = $shippingMethodConverter; } - public function getAvailableShippingMethods(QuoteAddress $address): array + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { - $methods = []; + if (!isset($value['model'])) { + throw new LocalizedException(__('"model" values should be specified')); + } + $address = $value['model']; // Allow shipping rates by setting country id for new addresses if (!$address->getCountryId() && $address->getCountryCode()) { @@ -51,6 +61,7 @@ public function getAvailableShippingMethods(QuoteAddress $address): array $address->collectShippingRates(); $cart = $address->getQuote(); + $methods = []; $shippingRates = $address->getGroupedAllShippingRates(); foreach ($shippingRates as $carrierRates) { foreach ($carrierRates as $rate) { @@ -61,7 +72,6 @@ public function getAvailableShippingMethods(QuoteAddress $address): array ); } } - return $methods; } } diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index c34adb08a17a..c52bd52fb322 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -2,7 +2,7 @@ # See COPYING.txt for license details. type Query { - Cart(cart_id: String!): Cart @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart") @doc(description:"Returns information about shopping cart") + cart(cart_id: String!): Cart @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart") @doc(description:"Returns information about shopping cart") } type Mutation { @@ -117,8 +117,8 @@ type CartAddress { country: CartAddressCountry telephone: String address_type: AdressTypeEnum - selected_shipping_method: CheckoutShippingMethod - available_shipping_methods: [CheckoutAvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddressShippingMethods") + selected_shipping_method: ShippingMethod + available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAdress\\AvailableShippingMethods") items_weight: Float customer_notes: String cart_items: [CartItemQuantity] @@ -139,7 +139,7 @@ type CartAddressCountry { label: String } -type CheckoutShippingMethod { +type ShippingMethod { code: String label: String free_shipping: Boolean! @@ -150,7 +150,7 @@ type CheckoutShippingMethod { base_amount_incl_tax: Float! } -type CheckoutAvailableShippingMethod { +type AvailableShippingMethod { carrier_code: String! carrier_title: String! method_code: String! @@ -160,7 +160,6 @@ type CheckoutAvailableShippingMethod { base_amount: Float! price_excl_tax: Float! price_incl_tax: Float! - available: Boolean! } enum AdressTypeEnum { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailableShippingMethodsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailableShippingMethodsTest.php new file mode 100644 index 000000000000..7b72afa15701 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetAvailableShippingMethodsTest.php @@ -0,0 +1,123 @@ +customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); + $this->quoteResource = $objectManager->create(QuoteResource::class); + $this->quote = $objectManager->create(Quote::class); + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php + */ + public function testGetAvailableShippingMethods() + { + $this->quoteResource->load( + $this->quote, + 'test_order_1', + 'reserved_order_id' + ); + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + + $query = <<graphQlQuery( + $query, + [], + '', + $this->getCustomerAuthHeaders('customer@example.com', 'password') + ); + self::assertArrayHasKey('cart', $response); + self::assertEquals($maskedQuoteId, $response['cart']['cart_id']); + self::assertArrayHasKey('shipping_addresses', $response['cart']); + self::assertCount(1, $response['cart']['shipping_addresses']); + self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]); + self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']); + + $expectedAddressData = [ + 'amount' => 10, + 'base_amount' => 10, + 'carrier_code' => 'flatrate', + 'carrier_title' => 'Flat Rate', + 'error_message' => '', + 'method_code' => 'flatrate', + 'method_title' => 'Fixed', + 'price_incl_tax' => 10, + 'price_excl_tax' => 10, + ]; + self::assertEquals( + $expectedAddressData, + $response['cart']['shipping_addresses'][0]['available_shipping_methods'][0] + ); + } + + /** + * @param string $email + * @param string $password + * @return array + */ + private function getCustomerAuthHeaders(string $email, string $password): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); + return ['Authorization' => 'Bearer ' . $customerToken]; + } +} From 506b9d27fabc4c218791d746d68f2b6de419e00b Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Thu, 14 Feb 2019 16:32:54 -0500 Subject: [PATCH 4/5] Remove `available` property from available shipping method test --- .../Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php index f4aaabc0876b..5bf5f3e03707 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php @@ -91,7 +91,6 @@ public function testSetNewGuestShippingAddressOnCart() telephone available_shipping_methods { amount - available base_amount carrier_code carrier_title @@ -369,7 +368,6 @@ public function testSetNewRegisteredCustomerShippingAddressOnCart() telephone available_shipping_methods { amount - available base_amount carrier_code carrier_title @@ -440,7 +438,6 @@ public function testSetSavedRegisteredCustomerShippingAddressOnCart() telephone available_shipping_methods { amount - available base_amount carrier_code carrier_title @@ -517,7 +514,6 @@ private function assertAvailableShippingRates(array $shippingAddressResponse): v $assertionMap = [ ['response_field' => 'amount', 'expected_value' => 5], - ['response_field' => 'available', 'expected_value' => true], ['response_field' => 'base_amount', 'expected_value' => 5], ['response_field' => 'carrier_code', 'expected_value' => 'flatrate'], ['response_field' => 'carrier_title', 'expected_value' => 'Flat Rate'], From 5ef26f7dcf5322c65886c435645e550c1606981f Mon Sep 17 00:00:00 2001 From: Patrick McLain Date: Thu, 14 Feb 2019 18:02:01 -0500 Subject: [PATCH 5/5] Adjust test for change in cart query casing --- .../testsuite/Magento/GraphQl/Quote/GetCartTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCartTest.php index f0038351bcdc..44cd2ef526bd 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/GetCartTest.php @@ -68,9 +68,9 @@ public function testGetOwnCartForRegisteredCustomer() $response = $this->sendRequestWithToken($query); - self::assertArrayHasKey('Cart', $response); - self::assertNotEmpty($response['Cart']['items']); - self::assertNotEmpty($response['Cart']['shipping_addresses']); + self::assertArrayHasKey('cart', $response); + self::assertNotEmpty($response['cart']['items']); + self::assertNotEmpty($response['cart']['shipping_addresses']); } /** @@ -110,7 +110,7 @@ public function testGetCartForGuest() $response = $this->graphQlQuery($query); - self::assertArrayHasKey('Cart', $response); + self::assertArrayHasKey('cart', $response); } public function testGetNonExistentCart() @@ -134,7 +134,7 @@ private function prepareGetCartQuery( ) : string { return <<