Skip to content

Commit 2aca9e6

Browse files
authored
Merge pull request #6343 from magento-honey-badgers/HB-PR-delivery-Nov
[honey] MC-37620: [GraphQl] WishListItemInterface implementation for GroupedProduct and incomplete implementation for GiftCardProduct
2 parents 77eb15e + 5e22308 commit 2aca9e6

File tree

16 files changed

+530
-91
lines changed

16 files changed

+530
-91
lines changed

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@
77

88
namespace Magento\CatalogCustomerGraphQl\Model\Resolver;
99

10+
use Magento\Catalog\Api\Data\ProductTierPriceInterface;
11+
use Magento\CatalogCustomerGraphQl\Model\Resolver\Customer\GetCustomerGroup;
12+
use Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\Tiers;
13+
use Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\TiersFactory;
14+
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount;
15+
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool;
16+
use Magento\Framework\Exception\LocalizedException;
1017
use Magento\Framework\GraphQl\Config\Element\Field;
18+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1119
use Magento\Framework\GraphQl\Query\ResolverInterface;
1220
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13-
use Magento\Framework\Exception\LocalizedException;
14-
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1521
use Magento\Framework\Pricing\PriceCurrencyInterface;
16-
use Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\Tiers;
17-
use Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\TiersFactory;
18-
use Magento\CatalogCustomerGraphQl\Model\Resolver\Customer\GetCustomerGroup;
1922
use Magento\Store\Api\Data\StoreInterface;
20-
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount;
21-
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool;
22-
use Magento\Catalog\Api\Data\ProductTierPriceInterface;
2323

2424
/**
2525
* Resolver for price_tiers
@@ -66,6 +66,16 @@ class PriceTiers implements ResolverInterface
6666
*/
6767
private $priceCurrency;
6868

69+
/**
70+
* @var array
71+
*/
72+
private $formatAndFilterTierPrices = [];
73+
74+
/**
75+
* @var array
76+
*/
77+
private $tierPricesQty = [];
78+
6979
/**
7080
* @param ValueFactory $valueFactory
7181
* @param TiersFactory $tiersFactory
@@ -115,52 +125,91 @@ public function resolve(
115125
return [];
116126
}
117127

118-
$productId = $product->getId();
128+
$productId = (int)$product->getId();
119129
$this->tiers->addProductFilter($productId);
120130

121131
return $this->valueFactory->create(
122132
function () use ($productId, $context) {
123-
/** @var StoreInterface $store */
124-
$store = $context->getExtensionAttributes()->getStore();
133+
$currencyCode = $context->getExtensionAttributes()->getStore()->getCurrentCurrencyCode();
125134

126135
$productPrice = $this->tiers->getProductRegularPrice($productId) ?? 0.0;
127136
$tierPrices = $this->tiers->getProductTierPrices($productId) ?? [];
128-
129-
return $this->formatProductTierPrices($tierPrices, $productPrice, $store);
137+
return $this->formatAndFilterTierPrices($productPrice, $tierPrices, $currencyCode);
130138
}
131139
);
132140
}
133141

134142
/**
135-
* Format tier prices for output
143+
* Format and filter tier prices for output
136144
*
137-
* @param ProductTierPriceInterface[] $tierPrices
138145
* @param float $productPrice
139-
* @param StoreInterface $store
146+
* @param ProductTierPriceInterface[] $tierPrices
147+
* @param string $currencyCode
140148
* @return array
141149
*/
142-
private function formatProductTierPrices(array $tierPrices, float $productPrice, StoreInterface $store): array
143-
{
144-
$tiers = [];
150+
private function formatAndFilterTierPrices(
151+
float $productPrice,
152+
array $tierPrices,
153+
string $currencyCode
154+
): array {
145155

146-
foreach ($tierPrices as $tierPrice) {
156+
foreach ($tierPrices as $key => $tierPrice) {
147157
$tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue()));
148-
$percentValue = $tierPrice->getExtensionAttributes()->getPercentageValue();
149-
if ($percentValue && is_numeric($percentValue)) {
150-
$discount = $this->discount->getDiscountByPercent($productPrice, (float)$percentValue);
158+
$this->formatTierPrices($productPrice, $currencyCode, $tierPrice);
159+
$this->filterTierPrices($tierPrices, $key, $tierPrice);
160+
}
161+
return $this->formatAndFilterTierPrices;
162+
}
163+
164+
/**
165+
* Format tier prices for output
166+
*
167+
* @param float $productPrice
168+
* @param string $currencyCode
169+
* @param ProductTierPriceInterface $tierPrice
170+
*/
171+
private function formatTierPrices(float $productPrice, string $currencyCode, $tierPrice)
172+
{
173+
$percentValue = $tierPrice->getExtensionAttributes()->getPercentageValue();
174+
if ($percentValue && is_numeric($percentValue)) {
175+
$discount = $this->discount->getDiscountByPercent($productPrice, (float)$percentValue);
176+
} else {
177+
$discount = $this->discount->getDiscountByDifference($productPrice, (float)$tierPrice->getValue());
178+
}
179+
180+
$this->formatAndFilterTierPrices[] = [
181+
"discount" => $discount,
182+
"quantity" => $tierPrice->getQty(),
183+
"final_price" => [
184+
"value" => $tierPrice->getValue(),
185+
"currency" => $currencyCode
186+
]
187+
];
188+
}
189+
190+
/**
191+
* Filter the lowest price for each quantity
192+
*
193+
* @param array $tierPrices
194+
* @param int $key
195+
* @param ProductTierPriceInterface $tierPriceItem
196+
*/
197+
private function filterTierPrices(
198+
array $tierPrices,
199+
int $key,
200+
ProductTierPriceInterface $tierPriceItem
201+
) {
202+
$qty = $tierPriceItem->getQty();
203+
if (isset($this->tierPricesQty[$qty])) {
204+
$priceQty = $this->tierPricesQty[$qty];
205+
if ((float)$tierPriceItem->getValue() < (float)$tierPrices[$priceQty]->getValue()) {
206+
unset($this->formatAndFilterTierPrices[$priceQty]);
207+
$this->tierPricesQty[$priceQty] = $key;
151208
} else {
152-
$discount = $this->discount->getDiscountByDifference($productPrice, (float)$tierPrice->getValue());
209+
unset($this->formatAndFilterTierPrices[$key]);
153210
}
154-
155-
$tiers[] = [
156-
"discount" => $discount,
157-
"quantity" => $tierPrice->getQty(),
158-
"final_price" => [
159-
"value" => $tierPrice->getValue(),
160-
"currency" => $store->getCurrentCurrencyCode()
161-
]
162-
];
211+
} else {
212+
$this->tierPricesQty[$qty] = $key;
163213
}
164-
return $tiers;
165214
}
166215
}

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/Product/Price/Tiers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __construct(
7777
*
7878
* @param int $productId
7979
*/
80-
public function addProductFilter($productId): void
80+
public function addProductFilter(int $productId): void
8181
{
8282
$this->filterProductIds[] = $productId;
8383
}

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/TierPrices.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function resolve(
8383

8484
/** @var Product $product */
8585
$product = $value['model'];
86-
$productId = $product->getId();
86+
$productId = (int)$product->getId();
8787
$this->tiers->addProductFilter($productId);
8888

8989
return $this->valueFactory->create(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ input ProductAttributeFilterInput @doc(description: "ProductAttributeFilterInput
326326
input CategoryFilterInput @doc(description: "CategoryFilterInput defines the filters to be used in the search. A filter contains at least one attribute, a comparison operator, and the value that is being searched for.")
327327
{
328328
ids: FilterEqualTypeInput @doc(description: "Filter by category ID that uniquely identifies the category.")
329+
parent_id: FilterEqualTypeInput @doc(description: "Filter by parent category ID")
329330
url_key: FilterEqualTypeInput @doc(description: "Filter by the part of the URL that identifies the category.")
330331
name: FilterMatchTypeInput @doc(description: "Filter by the display name of the category.")
331332
url_path: FilterEqualTypeInput @doc(description: "Filter by the URL path for the category.")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Eav\Model\ResourceModel\Entity\Attribute;
10+
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
14+
/**
15+
* Provide option value
16+
*/
17+
class OptionValueProvider
18+
{
19+
/**
20+
* @var AdapterInterface
21+
*/
22+
private $connection;
23+
24+
/**
25+
* @param ResourceConnection $connection
26+
*/
27+
public function __construct(ResourceConnection $connection)
28+
{
29+
$this->connection = $connection->getConnection();
30+
}
31+
32+
/**
33+
* Get EAV attribute option value by option id
34+
*
35+
* @param int $valueId
36+
* @return string|null
37+
*/
38+
public function get(int $valueId): ?string
39+
{
40+
$select = $this->connection->select()
41+
->from($this->connection->getTableName('eav_attribute_option_value'), 'value')
42+
->where('value_id = ?', $valueId);
43+
44+
$result = $this->connection->fetchOne($select);
45+
46+
if ($result !== false) {
47+
return $result;
48+
}
49+
50+
return null;
51+
}
52+
}

app/code/Magento/GroupedProductGraphQl/etc/graphql/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@
3737
</argument>
3838
</arguments>
3939
</type>
40+
41+
<type name="Magento\WishlistGraphQl\Model\Resolver\Type\WishlistItemType">
42+
<arguments>
43+
<argument name="supportedTypes" xsi:type="array">
44+
<item name="grouped" xsi:type="string">GroupedProductWishlistItem</item>
45+
</argument>
46+
</arguments>
47+
</type>
4048
</config>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ type GroupedProductItem @doc(description: "GroupedProductItem contains informati
1010
position: Int @doc(description: "The relative position of this item compared to the other group items")
1111
product: ProductInterface @doc(description: "The ProductInterface object, which contains details about this product option") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product")
1212
}
13+
14+
type GroupedProductWishlistItem implements WishlistItemInterface @doc(description: "A grouped product wish list item") {
15+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\SalesGraphQl\Model\Formatter;
9+
10+
use Magento\Sales\Api\Data\OrderInterface;
11+
use Magento\SalesGraphQl\Model\Order\OrderAddress;
12+
use Magento\SalesGraphQl\Model\Order\OrderPayments;
13+
14+
/**
15+
* Format order model for graphql schema
16+
*/
17+
class Order
18+
{
19+
/**
20+
* @var OrderAddress
21+
*/
22+
private $orderAddress;
23+
24+
/**
25+
* @var OrderPayments
26+
*/
27+
private $orderPayments;
28+
29+
/**
30+
* @param OrderAddress $orderAddress
31+
* @param OrderPayments $orderPayments
32+
*/
33+
public function __construct(
34+
OrderAddress $orderAddress,
35+
OrderPayments $orderPayments
36+
) {
37+
$this->orderAddress = $orderAddress;
38+
$this->orderPayments = $orderPayments;
39+
}
40+
41+
/**
42+
* Format order model for graphql schema
43+
*
44+
* @param OrderInterface $orderModel
45+
* @return array
46+
*/
47+
public function format(OrderInterface $orderModel): array
48+
{
49+
return [
50+
'created_at' => $orderModel->getCreatedAt(),
51+
'grand_total' => $orderModel->getGrandTotal(),
52+
'id' => base64_encode($orderModel->getEntityId()),
53+
'increment_id' => $orderModel->getIncrementId(),
54+
'number' => $orderModel->getIncrementId(),
55+
'order_date' => $orderModel->getCreatedAt(),
56+
'order_number' => $orderModel->getIncrementId(),
57+
'status' => $orderModel->getStatusLabel(),
58+
'shipping_method' => $orderModel->getShippingDescription(),
59+
'shipping_address' => $this->orderAddress->getOrderShippingAddress($orderModel),
60+
'billing_address' => $this->orderAddress->getOrderBillingAddress($orderModel),
61+
'payment_methods' => $this->orderPayments->getOrderPaymentMethod($orderModel),
62+
'model' => $orderModel,
63+
];
64+
}
65+
}

0 commit comments

Comments
 (0)