Skip to content

Commit e539c3a

Browse files
author
Prabhu Ram
committed
MC-31420: [GraphQL] Flat Rate shipping amount is returned as $10 instead of $5 for 1st configurable product in cart.
- Added fix and tests
1 parent 8ead858 commit e539c3a

File tree

3 files changed

+102
-7
lines changed

3 files changed

+102
-7
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/AvailableShippingMethods.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress;
99

10-
use Magento\Directory\Model\Currency;
1110
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1211
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\Framework\Exception\NoSuchEntityException;
1412
use Magento\Framework\GraphQl\Config\Element\Field;
1513
use Magento\Framework\GraphQl\Query\ResolverInterface;
1614
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1715
use Magento\Quote\Api\Data\ShippingMethodInterface;
1816
use Magento\Quote\Model\Cart\ShippingMethodConverter;
19-
use Magento\Store\Api\Data\StoreInterface;
17+
use Magento\Quote\Model\Quote\TotalsCollector;
2018

2119
/**
2220
* @inheritdoc
@@ -33,16 +31,24 @@ class AvailableShippingMethods implements ResolverInterface
3331
*/
3432
private $shippingMethodConverter;
3533

34+
/**
35+
* @var TotalsCollector
36+
*/
37+
private $totalsCollector;
38+
3639
/**
3740
* @param ExtensibleDataObjectConverter $dataObjectConverter
3841
* @param ShippingMethodConverter $shippingMethodConverter
42+
* @param TotalsCollector $totalsCollector
3943
*/
4044
public function __construct(
4145
ExtensibleDataObjectConverter $dataObjectConverter,
42-
ShippingMethodConverter $shippingMethodConverter
46+
ShippingMethodConverter $shippingMethodConverter,
47+
TotalsCollector $totalsCollector
4348
) {
4449
$this->dataObjectConverter = $dataObjectConverter;
4550
$this->shippingMethodConverter = $shippingMethodConverter;
51+
$this->totalsCollector = $totalsCollector;
4652
}
4753

4854
/**
@@ -61,9 +67,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6167
}
6268

6369
$address->setCollectShippingRates(true);
64-
$address->collectShippingRates();
6570
$cart = $address->getQuote();
66-
71+
$this->totalsCollector->collectAddressTotals($cart, $address);
6772
$methods = [];
6873
$shippingRates = $address->getGroupedAllShippingRates();
6974
foreach ($shippingRates as $carrierRates) {
@@ -88,7 +93,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8893
* @param array $data
8994
* @param string $quoteCurrencyCode
9095
* @return array
91-
* @throws NoSuchEntityException
9296
*/
9397
private function processMoneyTypeData(array $data, string $quoteCurrencyCode): array
9498
{

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,48 @@ public function testGetAvailableShippingMethods()
8282
);
8383
}
8484

85+
/**
86+
* Test case: get available shipping methods from current customer quote with configurable product
87+
*
88+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
89+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_address_quote_with_configurable_product.php
90+
*/
91+
public function testGetAvailableShippingMethodsWithConfigurableProduct()
92+
{
93+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_cart_with_configurable');
94+
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId));
95+
96+
self::assertArrayHasKey('cart', $response);
97+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
98+
self::assertCount(1, $response['cart']['shipping_addresses']);
99+
self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]);
100+
self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']);
101+
102+
$expectedAddressData = [
103+
'amount' => [
104+
'value' => 5,
105+
'currency' => 'USD',
106+
],
107+
'carrier_code' => 'flatrate',
108+
'carrier_title' => 'Flat Rate',
109+
'error_message' => '',
110+
'method_code' => 'flatrate',
111+
'method_title' => 'Fixed',
112+
'price_incl_tax' => [
113+
'value' => 5,
114+
'currency' => 'USD',
115+
],
116+
'price_excl_tax' => [
117+
'value' => 5,
118+
'currency' => 'USD',
119+
],
120+
];
121+
self::assertEquals(
122+
$expectedAddressData,
123+
$response['cart']['shipping_addresses'][0]['available_shipping_methods'][0]
124+
);
125+
}
126+
85127
/**
86128
* _security
87129
* @magentoApiDataFixture Magento/Customer/_files/customer.php

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,55 @@ public function testGetAvailableShippingMethods()
8181
);
8282
}
8383

84+
/**
85+
* Test case: get available shipping methods from current customer quote with configurable product
86+
*
87+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
88+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_address_quote_with_configurable_product.php
89+
*/
90+
public function testGetAvailableShippingMethodsWithConfigurableProduct()
91+
{
92+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_cart_with_configurable');
93+
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId));
94+
95+
self::assertArrayHasKey('cart', $response);
96+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
97+
self::assertCount(1, $response['cart']['shipping_addresses']);
98+
self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]);
99+
self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']);
100+
101+
$expectedAddressData = [
102+
'amount' => [
103+
'value' => 5,
104+
'currency' => 'USD',
105+
],
106+
'carrier_code' => 'flatrate',
107+
'carrier_title' => 'Flat Rate',
108+
'error_message' => '',
109+
'method_code' => 'flatrate',
110+
'method_title' => 'Fixed',
111+
'price_incl_tax' => [
112+
'value' => 5,
113+
'currency' => 'USD',
114+
],
115+
'price_excl_tax' => [
116+
'value' => 5,
117+
'currency' => 'USD',
118+
],
119+
'base_amount' => null,
120+
];
121+
self::assertEquals(
122+
$expectedAddressData,
123+
$response['cart']['shipping_addresses'][0]['available_shipping_methods'][0]
124+
);
125+
self::assertCount(2, $response['cart']['shipping_addresses'][0]['cart_items']);
126+
self::assertCount(2, $response['cart']['shipping_addresses'][0]['cart_items_v2']);
127+
self::assertEquals(
128+
'configurable',
129+
$response['cart']['shipping_addresses'][0]['cart_items_v2'][0]['product']['sku']
130+
);
131+
}
132+
84133
/**
85134
* _security
86135
* @magentoApiDataFixture Magento/Customer/_files/customer.php

0 commit comments

Comments
 (0)