Skip to content

Commit 41ba02d

Browse files
committed
#31206: Selected shipping method does not contain value uncluding tax - added values to schema price_excl_tax price_incl_tax
1 parent ad29452 commit 41ba02d

File tree

4 files changed

+204
-21
lines changed

4 files changed

+204
-21
lines changed

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
declare(strict_types=1);
78

89
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress;
910

10-
use Magento\Directory\Model\Currency;
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
1414
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Quote\Model\Cart\ShippingMethodConverter;
1516
use Magento\Quote\Model\Quote\Address;
1617
use Magento\Quote\Model\Quote\Address\Rate;
1718

@@ -20,6 +21,19 @@
2021
*/
2122
class SelectedShippingMethod implements ResolverInterface
2223
{
24+
/**
25+
* @var ShippingMethodConverter
26+
*/
27+
private $shippingMethodConverter;
28+
29+
/**
30+
* @param ShippingMethodConverter $shippingMethodConverter
31+
*/
32+
public function __construct(ShippingMethodConverter $shippingMethodConverter)
33+
{
34+
$this->shippingMethodConverter = $shippingMethodConverter;
35+
}
36+
2337
/**
2438
* @inheritdoc
2539
*/
@@ -31,8 +45,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
3145
/** @var Address $address */
3246
$address = $value['model'];
3347
$rates = $address->getAllShippingRates();
34-
$carrierTitle = '';
35-
$methodTitle = '';
3648

3749
if (!count($rates) || empty($address->getShippingMethod())) {
3850
return null;
@@ -42,26 +54,36 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4254

4355
/** @var Rate $rate */
4456
foreach ($rates as $rate) {
45-
if ($rate->getCode() == $address->getShippingMethod()) {
46-
$carrierTitle = $rate->getCarrierTitle();
47-
$methodTitle = $rate->getMethodTitle();
57+
if ($rate->getCode() === $address->getShippingMethod()) {
4858
break;
4959
}
5060
}
5161

52-
$data = [
62+
$cart = $address->getQuote();
63+
$selectedShippingMethod = $this->shippingMethodConverter->modelToDataObject(
64+
$rate,
65+
$cart->getQuoteCurrencyCode()
66+
);
67+
68+
return [
5369
'carrier_code' => $carrierCode,
5470
'method_code' => $methodCode,
55-
'carrier_title' => $carrierTitle,
56-
'method_title' => $methodTitle,
71+
'carrier_title' => $selectedShippingMethod->getCarrierTitle() ?? '',
72+
'method_title' => $selectedShippingMethod->getMethodTitle() ?? '',
5773
'amount' => [
5874
'value' => $address->getShippingAmount(),
59-
'currency' => $address->getQuote()->getQuoteCurrencyCode(),
75+
'currency' => $cart->getQuoteCurrencyCode(),
76+
],
77+
'price_excl_tax' => [
78+
'value' => $selectedShippingMethod->getPriceExclTax(),
79+
'currency' => $cart->getQuoteCurrencyCode(),
80+
],
81+
'price_incl_tax' => [
82+
'value' => $selectedShippingMethod->getPriceInclTax(),
83+
'currency' => $cart->getQuoteCurrencyCode(),
6084
],
6185
/** @deprecated The field should not be used on the storefront */
6286
'base_amount' => null,
6387
];
64-
65-
return $data;
6688
}
6789
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ type SelectedShippingMethod {
263263
method_title: String!
264264
amount: Money!
265265
base_amount: Money @deprecated(reason: "The field should not be used on the storefront")
266+
price_excl_tax: Money!
267+
price_incl_tax: Money!
266268
}
267269

268270
type AvailableShippingMethod {

app/code/Magento/Tax/Model/Config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,9 @@ public function getInfoUrl($store = null)
912912
public function needPriceConversion($store = null)
913913
{
914914
$res = false;
915-
$priceIncludesTax = $this->priceIncludesTax($store) || $this->getNeedUseShippingExcludeTax();
915+
$priceIncludesTax = $this->priceIncludesTax($store)
916+
|| $this->getNeedUseShippingExcludeTax()
917+
|| $this->shippingPriceIncludesTax($store);
916918
if ($priceIncludesTax) {
917919
switch ($this->getPriceDisplayType($store)) {
918920
case self::DISPLAY_TYPE_EXCLUDING_TAX:

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

Lines changed: 165 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\GraphQl\Quote\Customer;
99

10+
use Magento\Framework\Exception\AuthenticationException;
1011
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
1112
use Magento\Integration\Api\CustomerTokenServiceInterface;
1213
use Magento\TestFramework\Helper\Bootstrap;
@@ -38,14 +39,19 @@ protected function setUp(): void
3839
}
3940

4041
/**
42+
* @magentoConfigFixture default_store tax/calculation/shipping_includes_tax 1
43+
* @magentoConfigFixture default_store tax/cart_display/shipping 2
44+
* @magentoConfigFixture default_store tax/classes/shipping_tax_class 2
45+
* @magentoConfigFixture default_store tax/display/shipping 2
4146
* @magentoApiDataFixture Magento/Customer/_files/customer.php
4247
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
4348
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
4449
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
50+
* @magentoApiDataFixture Magento/Tax/_files/tax_rule_region_1_al.php
4551
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
4652
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
4753
*/
48-
public function testGetSelectedShippingMethod()
54+
public function testGetSelectedShippingMethodWithTax(): void
4955
{
5056
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
5157

@@ -78,16 +84,159 @@ public function testGetSelectedShippingMethod()
7884
self::assertEquals(10, $amount['value']);
7985
self::assertArrayHasKey('currency', $amount);
8086
self::assertEquals('USD', $amount['currency']);
87+
88+
self::assertArrayHasKey('price_excl_tax', $shippingAddress['selected_shipping_method']);
89+
$priceExclTax = $shippingAddress['selected_shipping_method']['price_excl_tax'];
90+
91+
self::assertArrayHasKey('value', $priceExclTax);
92+
self::assertEquals(10, $priceExclTax['value']);
93+
self::assertArrayHasKey('currency', $priceExclTax);
94+
self::assertEquals('USD', $priceExclTax['currency']);
95+
96+
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
97+
$priceInclTax = $shippingAddress['selected_shipping_method']['price_incl_tax'];
98+
99+
self::assertArrayHasKey('value', $priceInclTax);
100+
self::assertEquals(10.75, $priceInclTax['value']);
101+
self::assertArrayHasKey('currency', $priceInclTax);
102+
self::assertEquals('USD', $priceInclTax['currency']);
103+
}
104+
105+
/**
106+
* @magentoConfigFixture default_store tax/calculation/shipping_includes_tax 1
107+
* @magentoConfigFixture default_store tax/cart_display/shipping 2
108+
* @magentoConfigFixture default_store tax/classes/shipping_tax_class 2
109+
* @magentoConfigFixture default_store tax/display/shipping 2
110+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
111+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
112+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
113+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
114+
* @magentoApiDataFixture Magento/Tax/_files/tax_rule_region_1_al.php
115+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_canada_address.php
116+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
117+
*/
118+
public function testGetSelectedShippingMethodWithAddressWithoutTax(): void
119+
{
120+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
121+
122+
$query = $this->getQuery($maskedQuoteId);
123+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
124+
125+
self::assertArrayHasKey('cart', $response);
126+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
127+
self::assertCount(1, $response['cart']['shipping_addresses']);
128+
129+
$shippingAddress = current($response['cart']['shipping_addresses']);
130+
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
131+
132+
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
133+
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);
134+
135+
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
136+
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
137+
138+
self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
139+
self::assertEquals('Flat Rate', $shippingAddress['selected_shipping_method']['carrier_title']);
140+
141+
self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
142+
self::assertEquals('Fixed', $shippingAddress['selected_shipping_method']['method_title']);
143+
144+
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
145+
$amount = $shippingAddress['selected_shipping_method']['amount'];
146+
147+
self::assertArrayHasKey('value', $amount);
148+
self::assertEquals(10, $amount['value']);
149+
self::assertArrayHasKey('currency', $amount);
150+
self::assertEquals('USD', $amount['currency']);
151+
152+
self::assertArrayHasKey('price_excl_tax', $shippingAddress['selected_shipping_method']);
153+
$priceExclTax = $shippingAddress['selected_shipping_method']['price_excl_tax'];
154+
155+
self::assertArrayHasKey('value', $priceExclTax);
156+
self::assertEquals(10, $priceExclTax['value']);
157+
self::assertArrayHasKey('currency', $priceExclTax);
158+
self::assertEquals('USD', $priceExclTax['currency']);
159+
160+
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
161+
$priceInclTax = $shippingAddress['selected_shipping_method']['price_incl_tax'];
162+
163+
self::assertArrayHasKey('value', $priceInclTax);
164+
self::assertEquals(10, $priceInclTax['value']);
165+
self::assertArrayHasKey('currency', $priceInclTax);
166+
self::assertEquals('USD', $priceInclTax['currency']);
81167
}
82168

83169
/**
170+
* @magentoConfigFixture default_store tax/calculation/shipping_includes_tax 0
171+
* @magentoConfigFixture default_store tax/cart_display/shipping 1
172+
* @magentoConfigFixture default_store tax/classes/shipping_tax_class 0
173+
* @magentoConfigFixture default_store tax/display/shipping 1
84174
* @magentoApiDataFixture Magento/Customer/_files/customer.php
85175
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
86176
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
87177
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
88178
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
179+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
89180
*/
90-
public function testGetSelectedShippingMethodBeforeSet()
181+
public function testGetSelectedShippingMethodWithoutTax(): void
182+
{
183+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
184+
185+
$query = $this->getQuery($maskedQuoteId);
186+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
187+
188+
self::assertArrayHasKey('cart', $response);
189+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
190+
self::assertCount(1, $response['cart']['shipping_addresses']);
191+
192+
$shippingAddress = current($response['cart']['shipping_addresses']);
193+
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
194+
195+
self::assertArrayHasKey('carrier_code', $shippingAddress['selected_shipping_method']);
196+
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['carrier_code']);
197+
198+
self::assertArrayHasKey('method_code', $shippingAddress['selected_shipping_method']);
199+
self::assertEquals('flatrate', $shippingAddress['selected_shipping_method']['method_code']);
200+
201+
self::assertArrayHasKey('carrier_title', $shippingAddress['selected_shipping_method']);
202+
self::assertEquals('Flat Rate', $shippingAddress['selected_shipping_method']['carrier_title']);
203+
204+
self::assertArrayHasKey('method_title', $shippingAddress['selected_shipping_method']);
205+
self::assertEquals('Fixed', $shippingAddress['selected_shipping_method']['method_title']);
206+
207+
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
208+
$amount = $shippingAddress['selected_shipping_method']['amount'];
209+
210+
self::assertArrayHasKey('value', $amount);
211+
self::assertEquals(10, $amount['value']);
212+
self::assertArrayHasKey('currency', $amount);
213+
self::assertEquals('USD', $amount['currency']);
214+
215+
self::assertArrayHasKey('price_excl_tax', $shippingAddress['selected_shipping_method']);
216+
$priceExclTax = $shippingAddress['selected_shipping_method']['price_excl_tax'];
217+
218+
self::assertArrayHasKey('value', $priceExclTax);
219+
self::assertEquals(10, $priceExclTax['value']);
220+
self::assertArrayHasKey('currency', $priceExclTax);
221+
self::assertEquals('USD', $priceExclTax['currency']);
222+
223+
self::assertArrayHasKey('amount', $shippingAddress['selected_shipping_method']);
224+
$priceInclTax = $shippingAddress['selected_shipping_method']['price_incl_tax'];
225+
226+
self::assertArrayHasKey('value', $priceInclTax);
227+
self::assertEquals(10, $priceInclTax['value']);
228+
self::assertArrayHasKey('currency', $priceInclTax);
229+
self::assertEquals('USD', $priceInclTax['currency']);
230+
}
231+
232+
/**
233+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
234+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
235+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
236+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
237+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
238+
*/
239+
public function testGetSelectedShippingMethodBeforeSet(): void
91240
{
92241
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
93242

@@ -112,7 +261,7 @@ public function testGetSelectedShippingMethodBeforeSet()
112261
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
113262
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
114263
*/
115-
public function testGetSelectedShippingMethodFromGuestCart()
264+
public function testGetSelectedShippingMethodFromGuestCart(): void
116265
{
117266
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
118267
$query = $this->getQuery($maskedQuoteId);
@@ -132,7 +281,7 @@ public function testGetSelectedShippingMethodFromGuestCart()
132281
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
133282
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_flatrate_shipping_method.php
134283
*/
135-
public function testGetSelectedShippingMethodFromAnotherCustomerCart()
284+
public function testGetSelectedShippingMethodFromAnotherCustomerCart(): void
136285
{
137286
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
138287
$query = $this->getQuery($maskedQuoteId);
@@ -150,7 +299,7 @@ public function testGetSelectedShippingMethodFromAnotherCustomerCart()
150299
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
151300
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
152301
*/
153-
public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet()
302+
public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet(): void
154303
{
155304
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
156305
$query = $this->getQuery($maskedQuoteId);
@@ -170,7 +319,7 @@ public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet()
170319
* @magentoApiDataFixture Magento/Customer/_files/customer.php
171320
*
172321
*/
173-
public function testGetGetSelectedShippingMethodOfNonExistentCart()
322+
public function testGetGetSelectedShippingMethodOfNonExistentCart(): void
174323
{
175324
$this->expectException(\Exception::class);
176325
$this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"');
@@ -185,12 +334,12 @@ public function testGetGetSelectedShippingMethodOfNonExistentCart()
185334
* @param string $username
186335
* @param string $password
187336
* @return array
337+
* @throws AuthenticationException
188338
*/
189339
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
190340
{
191341
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
192-
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
193-
return $headerMap;
342+
return ['Authorization' => 'Bearer ' . $customerToken];
194343
}
195344

196345
/**
@@ -216,6 +365,14 @@ private function getQuery(string $maskedQuoteId): string
216365
value
217366
currency
218367
}
368+
price_excl_tax {
369+
value
370+
currency
371+
}
372+
price_incl_tax {
373+
value
374+
currency
375+
}
219376
}
220377
}
221378
}

0 commit comments

Comments
 (0)