Skip to content

Commit 4e53b87

Browse files
authored
LYNX-449: Include "cart summary" related fields in guestOrder/guestOrderByToken GQL
1 parent 61f17d1 commit 4e53b87

File tree

9 files changed

+917
-0
lines changed

9 files changed

+917
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesGraphQl\Model\OrderItemPrices;
9+
10+
use Magento\Framework\Pricing\PriceCurrencyInterface;
11+
use Magento\Sales\Model\Order\Item;
12+
13+
/**
14+
* Prices data provider for order item
15+
*/
16+
class PricesProvider
17+
{
18+
/**
19+
* @param PriceCurrencyInterface $priceCurrency
20+
*/
21+
public function __construct(
22+
private readonly PriceCurrencyInterface $priceCurrency
23+
) {
24+
}
25+
26+
/**
27+
* Returns an array of different prices applied on the order item
28+
*
29+
* @param Item $orderItem
30+
* @return array
31+
*/
32+
public function execute(Item $orderItem): array
33+
{
34+
$currency = $orderItem->getOrder()->getOrderCurrencyCode();
35+
return [
36+
'model' => $orderItem,
37+
'price' => [
38+
'currency' => $currency,
39+
'value' => $orderItem->getPrice() ?? 0
40+
],
41+
'price_including_tax' => [
42+
'currency' => $currency,
43+
'value' => $orderItem->getPriceInclTax() ?? 0
44+
],
45+
'row_total' => [
46+
'currency' => $currency,
47+
'value' => $orderItem->getRowTotal() ?? 0
48+
],
49+
'row_total_including_tax' => [
50+
'currency' => $currency,
51+
'value' => $orderItem->getRowTotalInclTax() ?? 0
52+
],
53+
'total_item_discount' => [
54+
'currency' => $currency,
55+
'value' => $orderItem->getDiscountAmount() ?? 0
56+
],
57+
'original_price' => [
58+
'currency' => $currency,
59+
'value' => $orderItem->getOriginalPrice()
60+
],
61+
'original_price_including_tax' => [
62+
'currency' => $currency,
63+
'value' => $this->getOriginalPriceInclTax($orderItem)
64+
],
65+
'original_row_total' => [
66+
'currency' => $currency,
67+
'value' => $this->getOriginalRowTotal($orderItem)
68+
],
69+
'original_row_total_including_tax' => [
70+
'currency' => $currency,
71+
'value' => $this->getOriginalRowTotalInclTax($orderItem)
72+
]
73+
];
74+
}
75+
76+
/**
77+
* Calculate the original price including tax
78+
*
79+
* @param Item $orderItem
80+
* @return float
81+
*/
82+
private function getOriginalPriceInclTax(Item $orderItem): float
83+
{
84+
return $orderItem->getOriginalPrice() * (1 + ($orderItem->getTaxPercent() / 100));
85+
}
86+
87+
/**
88+
* Calculate the original row total price including tax
89+
*
90+
* @param Item $orderItem
91+
* @return float
92+
*/
93+
private function getOriginalRowTotalInclTax(Item $orderItem): float
94+
{
95+
return $this->getOriginalRowTotal($orderItem) * (1 + ($orderItem->getTaxPercent() / 100));
96+
}
97+
98+
/**
99+
* Calculate the original price row total
100+
*
101+
* @param Item $orderItem
102+
* @return float
103+
*/
104+
private function getOriginalRowTotal(Item $orderItem): float
105+
{
106+
$qty = $orderItem->getQtyOrdered();
107+
// Round unit price before multiplying to prevent losing 1 cent on subtotal
108+
return $this->priceCurrency->round($orderItem->getOriginalPrice() + $this->getOptionsPrice($orderItem)) * $qty;
109+
}
110+
111+
/**
112+
* Get the product custom options price
113+
*
114+
* @param Item $orderItem
115+
* @return float
116+
*/
117+
private function getOptionsPrice(Item $orderItem): float
118+
{
119+
$price = 0.0;
120+
$optionIds = $orderItem->getProduct()->getCustomOption('option_ids');
121+
if (!$optionIds) {
122+
return $price;
123+
}
124+
foreach (explode(',', $optionIds->getValue() ?? '') as $optionId) {
125+
$option = $orderItem->getProduct()->getOptionById($optionId);
126+
if ($option) {
127+
$price += $option->getRegularPrice();
128+
}
129+
}
130+
131+
return $price;
132+
}
133+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Sales\Model\Order\Item;
15+
use Magento\SalesGraphQl\Model\OrderItemPrices\PricesProvider;
16+
17+
/**
18+
* Resolver for OrderItemInterface.prices
19+
*/
20+
class OrderItemPrices implements ResolverInterface
21+
{
22+
/**
23+
* @param PricesProvider $pricesProvider
24+
*/
25+
public function __construct(
26+
private readonly PricesProvider $pricesProvider
27+
) {
28+
}
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): array
34+
{
35+
if (!isset($value['model']) || !($value['model'] instanceof Item)) {
36+
throw new LocalizedException(__('"model" value should be specified'));
37+
}
38+
39+
/** @var Item $orderItem */
40+
$orderItem = $value['model'];
41+
42+
$itemPrices = $this->pricesProvider->execute($orderItem);
43+
$itemPrices['discounts'] = $value['discounts'];
44+
45+
return $itemPrices;
46+
}
47+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,25 @@ interface OrderItemInterface @doc(description: "Order item details.") @typeResol
117117
quantity_canceled: Float @doc(description: "The number of canceled items.")
118118
quantity_returned: Float @doc(description: "The number of returned items.")
119119
product: ProductInterface @doc(description: "The ProductInterface object, which contains details about the base product") @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\ProductResolver")
120+
prices: OrderItemPrices @doc(description: "Contains details about the price of the item, including taxes and discounts.") @resolver(class: "\\Magento\\SalesGraphQl\\Model\\Resolver\\OrderItemPrices")
120121
}
121122

122123
type OrderItem implements OrderItemInterface {
123124
}
124125

126+
type OrderItemPrices {
127+
price: Money! @doc(description: "The price of the item before any discounts were applied. The price that might include tax, depending on the configured display settings for cart.")
128+
price_including_tax: Money! @doc(description: "The price of the item before any discounts were applied. The price that might include tax, depending on the configured display settings for cart.")
129+
row_total: Money! @doc(description: "The value of the price multiplied by the quantity of the item.")
130+
row_total_including_tax: Money! @doc(description: "The value of `row_total` plus the tax applied to the item.")
131+
discounts: [Discount] @doc(description: "An array of discounts to be applied to the cart item.")
132+
total_item_discount: Money! @doc(description: "The total of all discounts applied to the item.")
133+
original_price: Money @doc(description: "The original price of the item.")
134+
original_price_including_tax: Money @doc(description: "The original price of the item including tax.")
135+
original_row_total: Money! @doc(description: "The value of the original price multiplied by the quantity of the item.")
136+
original_row_total_including_tax: Money! @doc(description: "The value of the original price multiplied by the quantity of the item including tax.")
137+
}
138+
125139
type OrderItemOption @doc(description: "Represents order item options like selected or entered.") {
126140
label: String! @doc(description: "The name of the option.")
127141
value: String! @doc(description: "The value of the option.")
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WeeeGraphQl\Model\FixedProductTaxes;
9+
10+
use Magento\Sales\Model\Order\Item;
11+
use Magento\Store\Api\Data\StoreInterface;
12+
use Magento\Tax\Helper\Data as TaxHelper;
13+
use Magento\Tax\Model\Config;
14+
use Magento\Weee\Helper\Data;
15+
16+
/**
17+
* FPT data provider for order item
18+
*/
19+
class PricesProvider
20+
{
21+
/**
22+
* @param Data $weeHelper
23+
* @param TaxHelper $taxHelper
24+
*/
25+
public function __construct(
26+
private readonly Data $weeHelper,
27+
private readonly TaxHelper $taxHelper
28+
) {
29+
}
30+
31+
/**
32+
* Returns an array of different FPTs applied on the order item
33+
*
34+
* @param Item $orderItem
35+
* @param StoreInterface $store
36+
* @return array
37+
*/
38+
public function execute(Item $orderItem, StoreInterface $store): array
39+
{
40+
if (!$this->weeHelper->isEnabled($store)) {
41+
return [];
42+
}
43+
44+
$prices = $this->weeHelper->getApplied($orderItem);
45+
$displayInclTaxes = $this->taxHelper->getPriceDisplayType($store) === Config::DISPLAY_TYPE_INCLUDING_TAX;
46+
$currency = $orderItem->getOrder()->getOrderCurrencyCode();
47+
48+
$fixedProductTaxes = [];
49+
foreach ($prices as $price) {
50+
$fixedProductTaxes[] = [
51+
'amount' => [
52+
'value' => $displayInclTaxes ? $price['amount_incl_tax'] : $price['amount'],
53+
'currency' => $currency,
54+
],
55+
'label' => $price['title']
56+
];
57+
}
58+
59+
return $fixedProductTaxes;
60+
}
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WeeeGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Sales\Model\Order\Item;
15+
use Magento\Store\Api\Data\StoreInterface;
16+
use Magento\WeeeGraphQl\Model\FixedProductTaxes\PricesProvider;
17+
18+
/**
19+
* Resolver for the fixed_product_taxes in OrderItemPrices
20+
*/
21+
class FixedProductTaxes implements ResolverInterface
22+
{
23+
/**
24+
* @param PricesProvider $pricesProvider
25+
*/
26+
public function __construct(
27+
private readonly PricesProvider $pricesProvider
28+
) {
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): array
35+
{
36+
if (!isset($value['model']) || !($value['model'] instanceof Item)) {
37+
throw new LocalizedException(__('"model" value should be specified'));
38+
}
39+
40+
/** @var Item $orderItem */
41+
$orderItem = $value['model'];
42+
/** @var StoreInterface $store */
43+
$store = $context->getExtensionAttributes()->getStore();
44+
45+
return $this->pricesProvider->execute($orderItem, $store);
46+
}
47+
}

app/code/Magento/WeeeGraphQl/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"require": {
66
"php": "~8.1.0||~8.2.0||~8.3.0",
77
"magento/framework": "*",
8+
"magento/module-sales": "*",
89
"magento/module-store": "*",
910
"magento/module-tax": "*",
1011
"magento/module-weee": "*"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ enum FixedProductTaxDisplaySettings @doc(description: "Lists display settings fo
3232
EXCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values from `ProductPrice.fixed_product_taxes` are not displayed. This value corresponds to 'Excluding FPT'.")
3333
FPT_DISABLED @doc(description: "The FPT feature is not enabled. You can omit `ProductPrice.fixed_product_taxes` from your query.")
3434
}
35+
36+
type OrderItemPrices {
37+
fixed_product_taxes: [FixedProductTax!]! @resolver(class: "\\Magento\\WeeeGraphQl\\Model\\Resolver\\FixedProductTaxes")
38+
}

0 commit comments

Comments
 (0)