Skip to content

Commit 0c80bc4

Browse files
committed
LYNX-387: Add original row total price (#229)
1 parent 1438c4c commit 0c80bc4

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

app/code/Magento/Catalog/Test/Fixture/Product.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class Product implements RevertibleDataFixtureInterface
3636
'visibility' => Visibility::VISIBILITY_BOTH,
3737
'status' => Status::STATUS_ENABLED,
3838
'custom_attributes' => [
39-
'tax_class_id' => '2'
39+
'tax_class_id' => '2',
40+
'special_price' => null,
4041
],
4142
'extension_attributes' => [
4243
'website_ids' => [1],

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
15+
use Magento\Framework\Pricing\PriceCurrencyInterface;
1516
use Magento\Quote\Model\Cart\Totals;
1617
use Magento\Quote\Model\Quote\Item;
1718
use Magento\QuoteGraphQl\Model\Cart\TotalsCollector;
@@ -30,10 +31,12 @@ class CartItemPrices implements ResolverInterface, ResetAfterRequestInterface
3031
/**
3132
* @param TotalsCollector $totalsCollector
3233
* @param GetDiscounts $getDiscounts
34+
* @param PriceCurrencyInterface $priceCurrency
3335
*/
3436
public function __construct(
3537
private readonly TotalsCollector $totalsCollector,
36-
private readonly GetDiscounts $getDiscounts
38+
private readonly GetDiscounts $getDiscounts,
39+
private readonly PriceCurrencyInterface $priceCurrency
3740
) {
3841
}
3942

@@ -97,7 +100,24 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
97100
'discounts' => $this->getDiscounts->execute(
98101
$cartItem->getQuote(),
99102
$cartItem->getExtensionAttributes()->getDiscounts() ?? []
100-
)
103+
),
104+
'original_row_total' => [
105+
'currency' => $currencyCode,
106+
'value' => $this->getOriginalRowTotal($cartItem),
107+
],
101108
];
102109
}
110+
111+
/**
112+
* Calculate the original price row total
113+
*
114+
* @param Item $cartItem
115+
* @return float
116+
*/
117+
private function getOriginalRowTotal(Item $cartItem): float
118+
{
119+
$qty = $cartItem->getTotalQty();
120+
// Round unit price before multiplying to prevent losing 1 cent on subtotal
121+
return $this->priceCurrency->round($cartItem->getOriginalPrice()) * $qty;
122+
}
103123
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ type CartItemPrices @doc(description: "Contains details about the price of the i
452452
row_total_including_tax: Money! @doc(description: "The value of `row_total` plus the tax applied to the item.")
453453
discounts: [Discount] @doc(description: "An array of discounts to be applied to the cart item.")
454454
total_item_discount: Money @doc(description: "The total of all discounts applied to the item.")
455+
original_row_total: Money! @doc(description: "The value of the original price multiplied by the quantity of the item.")
455456
}
456457

457458
type SelectedCustomizableOption @doc(description: "Identifies a customized product that has been placed in a cart.") {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,40 @@ public function testGetTotalsWithNoTaxApplied()
266266
self::assertEmpty($pricesResponse['applied_taxes']);
267267
}
268268

269+
#[
270+
DataFixture(ProductFixture::class, [
271+
'price' => 15,
272+
'custom_attributes' => [
273+
'special_price' => 10
274+
]
275+
], 'p'),
276+
DataFixture(GuestCartFixture::class, as: 'cart'),
277+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$p.id$', 'qty' => 2]),
278+
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
279+
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
280+
]
281+
public function testGetTotalsWithOriginalRowTotalPrice()
282+
{
283+
$cart = $this->fixtures->get('cart');
284+
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
285+
$query = $this->getQuery($maskedQuoteId);
286+
$response = $this->graphQlQuery($query);
287+
288+
$cartItem = $response['cart']['items'][0];
289+
self::assertEquals(10, $cartItem['prices']['price']['value']);
290+
self::assertEquals(10, $cartItem['prices']['price_including_tax']['value']);
291+
self::assertEquals(20, $cartItem['prices']['row_total']['value']);
292+
self::assertEquals(20, $cartItem['prices']['row_total_including_tax']['value']);
293+
self::assertEquals(30, $cartItem['prices']['original_row_total']['value']);
294+
295+
$pricesResponse = $response['cart']['prices'];
296+
self::assertEquals(20, $pricesResponse['grand_total']['value']);
297+
self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']);
298+
self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']);
299+
self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']);
300+
self::assertEmpty($pricesResponse['applied_taxes']);
301+
}
302+
269303
/**
270304
* The totals calculation is based on quote address.
271305
* But the totals should be calculated even if no address is set
@@ -373,6 +407,10 @@ private function getQuery(string $maskedQuoteId): string
373407
value
374408
}
375409
}
410+
original_row_total {
411+
value
412+
currency
413+
}
376414
}
377415
}
378416
prices {

0 commit comments

Comments
 (0)