From 23b17a5ed8ebe8293f41a3abe6c96832981f17dd Mon Sep 17 00:00:00 2001 From: eliseacornejo Date: Tue, 16 Apr 2024 14:27:14 +0200 Subject: [PATCH] LYNX-387: Add original row total price (#229) --- .../Magento/Catalog/Test/Fixture/Product.php | 3 +- .../Model/Resolver/CartItemPrices.php | 24 +++++++++++- .../Magento/QuoteGraphQl/etc/schema.graphqls | 1 + .../GraphQl/Quote/Guest/CartTotalsTest.php | 38 +++++++++++++++++++ 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Fixture/Product.php b/app/code/Magento/Catalog/Test/Fixture/Product.php index 978dc7026b46d..bdbab7a14923c 100644 --- a/app/code/Magento/Catalog/Test/Fixture/Product.php +++ b/app/code/Magento/Catalog/Test/Fixture/Product.php @@ -36,7 +36,8 @@ class Product implements RevertibleDataFixtureInterface 'visibility' => Visibility::VISIBILITY_BOTH, 'status' => Status::STATUS_ENABLED, 'custom_attributes' => [ - 'tax_class_id' => '2' + 'tax_class_id' => '2', + 'special_price' => null, ], 'extension_attributes' => [ 'website_ids' => [1], diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php index 222a30548fbd1..9d5a83c31ce10 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php @@ -12,6 +12,7 @@ use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\ObjectManager\ResetAfterRequestInterface; +use Magento\Framework\Pricing\PriceCurrencyInterface; use Magento\Quote\Model\Cart\Totals; use Magento\Quote\Model\Quote\Item; use Magento\QuoteGraphQl\Model\Cart\TotalsCollector; @@ -30,10 +31,12 @@ class CartItemPrices implements ResolverInterface, ResetAfterRequestInterface /** * @param TotalsCollector $totalsCollector * @param GetDiscounts $getDiscounts + * @param PriceCurrencyInterface $priceCurrency */ public function __construct( private readonly TotalsCollector $totalsCollector, - private readonly GetDiscounts $getDiscounts + private readonly GetDiscounts $getDiscounts, + private readonly PriceCurrencyInterface $priceCurrency ) { } @@ -97,7 +100,24 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value 'discounts' => $this->getDiscounts->execute( $cartItem->getQuote(), $cartItem->getExtensionAttributes()->getDiscounts() ?? [] - ) + ), + 'original_row_total' => [ + 'currency' => $currencyCode, + 'value' => $this->getOriginalRowTotal($cartItem), + ], ]; } + + /** + * Calculate the original price row total + * + * @param Item $cartItem + * @return float + */ + private function getOriginalRowTotal(Item $cartItem): float + { + $qty = $cartItem->getTotalQty(); + // Round unit price before multiplying to prevent losing 1 cent on subtotal + return $this->priceCurrency->round($cartItem->getOriginalPrice()) * $qty; + } } diff --git a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls index 53da2d8751127..ed3f865155cbe 100644 --- a/app/code/Magento/QuoteGraphQl/etc/schema.graphqls +++ b/app/code/Magento/QuoteGraphQl/etc/schema.graphqls @@ -452,6 +452,7 @@ type CartItemPrices @doc(description: "Contains details about the price of the i row_total_including_tax: Money! @doc(description: "The value of `row_total` plus the tax applied to the item.") discounts: [Discount] @doc(description: "An array of discounts to be applied to the cart item.") total_item_discount: Money @doc(description: "The total of all discounts applied to the item.") + original_row_total: Money! @doc(description: "The value of the original price multiplied by the quantity of the item.") } type SelectedCustomizableOption @doc(description: "Identifies a customized product that has been placed in a cart.") { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php index f172e32c60c69..dc478c45baa6c 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartTotalsTest.php @@ -266,6 +266,40 @@ public function testGetTotalsWithNoTaxApplied() self::assertEmpty($pricesResponse['applied_taxes']); } + #[ + DataFixture(ProductFixture::class, [ + 'price' => 15, + 'custom_attributes' => [ + 'special_price' => 10 + ] + ], 'p'), + DataFixture(GuestCartFixture::class, as: 'cart'), + DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$p.id$', 'qty' => 2]), + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']), + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']), + ] + public function testGetTotalsWithOriginalRowTotalPrice() + { + $cart = $this->fixtures->get('cart'); + $maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId()); + $query = $this->getQuery($maskedQuoteId); + $response = $this->graphQlQuery($query); + + $cartItem = $response['cart']['items'][0]; + self::assertEquals(10, $cartItem['prices']['price']['value']); + self::assertEquals(10, $cartItem['prices']['price_including_tax']['value']); + self::assertEquals(20, $cartItem['prices']['row_total']['value']); + self::assertEquals(20, $cartItem['prices']['row_total_including_tax']['value']); + self::assertEquals(30, $cartItem['prices']['original_row_total']['value']); + + $pricesResponse = $response['cart']['prices']; + self::assertEquals(20, $pricesResponse['grand_total']['value']); + self::assertEquals(20, $pricesResponse['subtotal_including_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_excluding_tax']['value']); + self::assertEquals(20, $pricesResponse['subtotal_with_discount_excluding_tax']['value']); + self::assertEmpty($pricesResponse['applied_taxes']); + } + /** * The totals calculation is based on quote address. * But the totals should be calculated even if no address is set @@ -373,6 +407,10 @@ private function getQuery(string $maskedQuoteId): string value } } + original_row_total { + value + currency + } } } prices {