Skip to content

Commit 2261e6c

Browse files
authored
LYNX-512: original_item_price is not including discounts
1 parent a7d43ce commit 2261e6c

File tree

5 files changed

+244
-7
lines changed

5 files changed

+244
-7
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8080
} else {
8181
$discountAmount = $cartItem->getDiscountAmount();
8282
}
83+
84+
/**
85+
* Calculate the actual price of the product with all discounts applied
86+
*/
87+
$originalItemPrice = $cartItem->getTotalDiscountAmount() > 0
88+
? $this->priceCurrency->round(
89+
$cartItem->getCalculationPrice() - ($cartItem->getTotalDiscountAmount() / max($cartItem->getQty(), 1))
90+
)
91+
: $cartItem->getCalculationPrice();
92+
8393
return [
8494
'model' => $cartItem,
8595
'price' => [
@@ -106,6 +116,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
106116
$cartItem->getQuote(),
107117
$cartItem->getExtensionAttributes()->getDiscounts() ?? []
108118
),
119+
'original_item_price' => [
120+
'currency' => $currencyCode,
121+
'value' => $originalItemPrice
122+
],
109123
'original_row_total' => [
110124
'currency' => $currencyCode,
111125
'value' => $this->getOriginalRowTotal($cartItem),

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

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

dev/tests/api-functional/testsuite/Magento/GraphQl/Bundle/BundleProductCartPricesTest.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public function testBundleProductFixedPriceWithOptionsWithoutPrices()
115115

116116
// price is the bundle product price as in this case the options don't have prices
117117
// specialPrice is the bundle product price * bundle product special price %
118-
$expectedResponse = $this->getExpectedResponse(15, 30, 30, 13.5, 27);
118+
// originalItemPriceProduct1 is the bundle product price
119+
// originalItemPriceProduct1 is with 10% discount as the special price
120+
$expectedResponse = $this->getExpectedResponse(15, 30, 30, 13.5, 27, 15, 13.5);
119121

120122
$this->assertEquals($expectedResponse, $response);
121123
}
@@ -185,7 +187,9 @@ public function testBundleProductFixedPriceWithOneOptionFixedPrice()
185187

186188
// price is the bundle product price + option fixed price
187189
// specialPrice is the bundle product price + option fixed price * bundle product special price %
188-
$expectedResponse = $this->getExpectedResponse(25, 50, 50, 22.5, 45);
190+
// originalItemPriceProduct1 is the bundle product price
191+
// originalItemPriceProduct1 is with 10% discount as the special price
192+
$expectedResponse = $this->getExpectedResponse(25, 50, 50, 22.5, 45, 25, 22.5);
189193

190194
$this->assertEquals($expectedResponse, $response);
191195
}
@@ -263,7 +267,9 @@ public function testBundleProductFixedPriceWithBothOptionsFixedPrice()
263267

264268
// price is the bundle product price + options fixed prices
265269
// specialPrice is the bundle product price + options fixed prices * bundle product special price %
266-
$expectedResponse = $this->getExpectedResponse(45, 90, 90, 40.50, 81);
270+
// originalItemPriceProduct1 is the bundle product price
271+
// originalItemPriceProduct1 is with 10% discount as the special price
272+
$expectedResponse = $this->getExpectedResponse(45, 90, 90, 40.50, 81, 45, 40.5);
267273

268274
$this->assertEquals($expectedResponse, $response);
269275
}
@@ -334,7 +340,9 @@ public function testBundleProductFixedPriceWithOneOptionPercentPrice()
334340
// price is the (bundle product price * option percent price) + bundle product price
335341
// specialPrice is the (bundle product price * option percent price) +
336342
// bundle product price * bundle product special price %
337-
$expectedResponse = $this->getExpectedResponse(18, 36, 36, 16.20, 32.40);
343+
// originalItemPriceProduct1 is the bundle product price
344+
// originalItemPriceProduct1 is with 10% discount as the special price
345+
$expectedResponse = $this->getExpectedResponse(18, 36, 36, 16.20, 32.40, 18, 16.2);
338346

339347
$this->assertEquals($expectedResponse, $response);
340348
}
@@ -413,7 +421,9 @@ public function testBundleProductFixedPriceWithBothOptionsPercentPrices()
413421
// price is the (bundle product price * options percent price) + bundle product price
414422
// specialPrice is the (bundle product price * options percent price) +
415423
// bundle product price * bundle product special price %
416-
$expectedResponse = $this->getExpectedResponse(19.5, 39, 39, 17.55, 35.10);
424+
// originalItemPriceProduct1 is the bundle product price
425+
// originalItemPriceProduct1 is with 10% discount as the special price
426+
$expectedResponse = $this->getExpectedResponse(19.5, 39, 39, 17.55, 35.10, 19.5, 17.55);
417427

418428
$this->assertEquals($expectedResponse, $response);
419429
}
@@ -492,7 +502,9 @@ public function testBundleProductFixedPriceWithOneOptionFixedAndOnePercentPrice(
492502
// price is the (bundle product price * option percent price) + bundle product price + option fixed price
493503
// specialPrice is the (bundle product price * option percent price) + bundle product price +
494504
// option fixed price * bundle product special price %
495-
$expectedResponse = $this->getExpectedResponse(28, 56, 56, 25.20, 50.40);
505+
// originalItemPriceProduct1 is the bundle product price
506+
// originalItemPriceProduct1 is with 10% discount as the special price
507+
$expectedResponse = $this->getExpectedResponse(28, 56, 56, 25.20, 50.40, 28, 25.2);
496508

497509
$this->assertEquals($expectedResponse, $response);
498510
}
@@ -546,6 +558,10 @@ public function testBundleProductDynamicPriceWithoutSpecialPrice()
546558
"original_row_total" => [
547559
"value" => 60,
548560
"currency" => "USD"
561+
],
562+
"original_item_price" => [
563+
"value" => 30,
564+
"currency" => "USD"
549565
]
550566
]
551567
]
@@ -605,6 +621,10 @@ public function testBundleProductDynamicPriceWithSpecialPrice()
605621
"original_row_total" => [
606622
"value" => 60,
607623
"currency" => "USD"
624+
],
625+
"original_item_price" => [
626+
"value" => 25, // product 1 special_price(15) + product 2 price (10)
627+
"currency" => "USD"
608628
]
609629
]
610630
]
@@ -640,6 +660,10 @@ private function getCartQuery(string $maskedQuoteId): string
640660
value
641661
currency
642662
}
663+
original_item_price {
664+
value
665+
currency
666+
}
643667
}
644668
}
645669
}
@@ -653,14 +677,18 @@ private function getCartQuery(string $maskedQuoteId): string
653677
* @param $originalRowTotal
654678
* @param $specialPrice
655679
* @param $specialRowTotal
680+
* @param $originalItemPriceProduct1
681+
* @param $originalItemPriceProduct2
656682
* @return array[]
657683
*/
658684
private function getExpectedResponse(
659685
$price,
660686
$rowTotal,
661687
$originalRowTotal,
662688
$specialPrice,
663-
$specialRowTotal
689+
$specialRowTotal,
690+
$originalItemPriceProduct1,
691+
$originalItemPriceProduct2
664692
): array {
665693
return [
666694
"cart" => [
@@ -678,6 +706,10 @@ private function getExpectedResponse(
678706
"original_row_total" => [
679707
"value" => $originalRowTotal,
680708
"currency" => "USD"
709+
],
710+
"original_item_price" => [
711+
"value" => $originalItemPriceProduct1,
712+
"currency" => "USD"
681713
]
682714
]
683715
],
@@ -694,6 +726,10 @@ private function getExpectedResponse(
694726
"original_row_total" => [
695727
"value" => $originalRowTotal,
696728
"currency" => "USD"
729+
],
730+
"original_item_price" => [
731+
"value" => $originalItemPriceProduct2,
732+
"currency" => "USD"
697733
]
698734
]
699735
]
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\GraphQl\Quote\Customer;
18+
19+
use Exception;
20+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
21+
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture;
22+
use Magento\Customer\Test\Fixture\Customer as CustomerFixture;
23+
use Magento\Framework\Exception\AuthenticationException;
24+
use Magento\Framework\Exception\LocalizedException;
25+
use Magento\Integration\Api\CustomerTokenServiceInterface;
26+
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
27+
use Magento\Quote\Test\Fixture\CustomerCart as CustomerCartFixture;
28+
use Magento\Quote\Test\Fixture\QuoteIdMask;
29+
use Magento\SalesRule\Model\Rule as SalesRule;
30+
use Magento\SalesRule\Test\Fixture\AddressCondition as AddressConditionFixture;
31+
use Magento\SalesRule\Test\Fixture\Rule as SalesRuleFixture;
32+
use Magento\TestFramework\Fixture\DataFixture;
33+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
34+
use Magento\TestFramework\Helper\Bootstrap;
35+
use Magento\TestFramework\TestCase\GraphQlAbstract;
36+
37+
/**
38+
* Test getting CartItemPrices schema (original_item_price & original_row_total) fields
39+
*/
40+
class CartItemPriceTest extends GraphQlAbstract
41+
{
42+
/**
43+
* @var CustomerTokenServiceInterface
44+
*/
45+
private $customerTokenService;
46+
47+
protected function setUp(): void
48+
{
49+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
50+
}
51+
52+
/**
53+
* Test original_item_price & original_row_total fields
54+
*
55+
* @return void
56+
* @throws AuthenticationException
57+
* @throws LocalizedException
58+
* @throws Exception
59+
*/
60+
#[
61+
DataFixture(
62+
AddressConditionFixture::class,
63+
['attribute' => 'base_subtotal', 'operator' => '>=', 'value' => 0],
64+
'condition'
65+
),
66+
DataFixture(
67+
SalesRuleFixture::class,
68+
[
69+
'store_labels' => [1 => 'Coupon1'],
70+
'simple_action' => SalesRule::BY_PERCENT_ACTION,
71+
'uses_per_customer' => 1,
72+
'discount_amount' => 10,
73+
'stop_rules_processing' => false,
74+
'conditions' => ['$condition$']
75+
]
76+
),
77+
DataFixture(
78+
SalesRuleFixture::class,
79+
[
80+
'store_labels' => [1 => 'Coupon2'],
81+
'simple_action' => SalesRule::BY_FIXED_ACTION,
82+
'uses_per_customer' => 1,
83+
'discount_amount' => 0.5,
84+
'stop_rules_processing' => false,
85+
'conditions' => ['$condition$']
86+
]
87+
),
88+
DataFixture(ProductFixture::class, as: 'product'),
89+
DataFixture(CustomerFixture::class, ['email' => 'customer@example.com'], as: 'customer'),
90+
DataFixture(CustomerCartFixture::class, ['customer_id' => '$customer.id$'], as: 'cart'),
91+
DataFixture(
92+
AddProductToCartFixture::class,
93+
['cart_id' => '$cart.id$', 'product_id' => '$product.id$', 'qty' => 2]
94+
),
95+
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
96+
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
97+
]
98+
public function testGetCartItemPricesWithDiscount()
99+
{
100+
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();
101+
$query = $this->getQuery($maskedQuoteId);
102+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
103+
104+
self::assertEquals(
105+
[
106+
'cart' => [
107+
'items' => [
108+
0 => [
109+
'prices' => [
110+
'original_item_price' => [
111+
'value' => 8.5,
112+
'currency' => 'USD'
113+
],
114+
'original_row_total' => [
115+
'value' => 20,
116+
'currency' => 'USD'
117+
]
118+
]
119+
]
120+
]
121+
]
122+
],
123+
$response
124+
);
125+
}
126+
127+
/**
128+
* Generates GraphQl query for retrieving cart items prices [original_item_price & original_row_total]
129+
*
130+
* @param string $maskedQuoteId
131+
* @return string
132+
*/
133+
private function getQuery(string $maskedQuoteId): string
134+
{
135+
return <<<QUERY
136+
{
137+
cart(cart_id: "$maskedQuoteId") {
138+
items {
139+
prices {
140+
original_item_price {
141+
value
142+
currency
143+
}
144+
original_row_total {
145+
value
146+
currency
147+
}
148+
}
149+
}
150+
}
151+
}
152+
QUERY;
153+
}
154+
155+
/**
156+
* Get Authentication header
157+
*
158+
* @return array
159+
* @throws AuthenticationException
160+
*/
161+
private function getHeaderMap(): array
162+
{
163+
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
164+
return ['Authorization' => 'Bearer ' . $customerToken];
165+
}
166+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function testProductsWithOneCustomOptionEnteredWithFixedPrice()
132132
"original_row_total" => [
133133
"value" => 80,
134134
"currency" => "USD"
135+
],
136+
"original_item_price" => [
137+
"value" => 25,
138+
"currency" => "USD"
135139
]
136140
]
137141
]
@@ -213,6 +217,10 @@ public function testProductsWithOneCustomOptionEnteredWithPercentPrice()
213217
"original_row_total" => [
214218
"value" => 66,
215219
"currency" => "USD"
220+
],
221+
"original_item_price" => [
222+
"value" => 16.5,
223+
"currency" => "USD"
216224
]
217225
]
218226
]
@@ -294,6 +302,10 @@ public function testProductsWithOneCustomOptionEnteredWithPercentPriceAndOneWith
294302
"original_row_total" => [
295303
"value" => 166,
296304
"currency" => "USD"
305+
],
306+
"original_item_price" => [
307+
"value" => 66.5,
308+
"currency" => "USD"
297309
]
298310
]
299311
]
@@ -435,6 +447,10 @@ public function testCartWithMultipleCustomProductOption()
435447
"original_row_total" => [
436448
"value" => 92,
437449
"currency" => "USD"
450+
],
451+
"original_item_price" => [
452+
"value" => 22,
453+
"currency" => "USD"
438454
]
439455
]
440456
]
@@ -472,6 +488,10 @@ private function getCartQuery(string $maskedQuoteId): string
472488
value
473489
currency
474490
}
491+
original_item_price {
492+
value
493+
currency
494+
}
475495
}
476496
}
477497
}

0 commit comments

Comments
 (0)