Skip to content

Commit

Permalink
LYNX-387: Add original row total price (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseacornejo authored and glo74186 committed May 7, 2024
1 parent 21f483a commit 23b17a5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/Catalog/Test/Fixture/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
24 changes: 22 additions & 2 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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.") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -373,6 +407,10 @@ private function getQuery(string $maskedQuoteId): string
value
}
}
original_row_total {
value
currency
}
}
}
prices {
Expand Down

0 comments on commit 23b17a5

Please sign in to comment.