Skip to content

Commit 3dbd757

Browse files
committed
Merge remote-tracking branch 'origin/BUG#AC-9058-v2' into spartans_pr_06092023
2 parents c39b71b + 7c706c0 commit 3dbd757

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\BundleGraphQl\Model\Resolver;
9+
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
16+
class BundlePriceDetails implements ResolverInterface
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
22+
{
23+
if (!isset($value['model'])) {
24+
throw new LocalizedException(__('"model" value should be specified'));
25+
}
26+
/** @var Product $product */
27+
$product = $value['model'];
28+
29+
$price = $product->getPrice();
30+
$finalPrice = $product->getFinalPrice();
31+
$discountPercentage = 100 - (($finalPrice * 100) / $price);
32+
return [
33+
'main_price' => $price,
34+
'main_final_price' => $finalPrice,
35+
'discount_percentage' => $discountPercentage
36+
];
37+
}
38+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ type SelectedBundleOptionValue @doc(description: "Contains details about a value
4747
price: Float! @doc(description: "The price of the value for the selected bundle product option.")
4848
}
4949

50+
type PriceDetails @doc(description: "Can be used to retrieve the main price details in case of bundle product") {
51+
main_price: Float @doc(description: "The regular price of the main product")
52+
main_final_price: Float @doc(description: "The final price after applying the discount to the main product")
53+
discount_percentage: Float @doc(description: "The percentage of discount applied to the main product price")
54+
}
55+
5056
type BundleItem @doc(description: "Defines an individual item within a bundle product.") {
5157
option_id: Int @deprecated(reason: "Use `uid` instead") @doc(description: "An ID assigned to each type of item in a bundle product.")
5258
uid: ID @doc(description: "The unique ID for a `BundleItem` object.")
@@ -79,6 +85,7 @@ type BundleProduct implements ProductInterface, RoutableInterface, PhysicalProdu
7985
dynamic_sku: Boolean @doc(description: "Indicates whether the bundle product has a dynamic SKU.") @resolver(class: "Magento\\BundleGraphQl\\Model\\Resolver\\Product\\Fields\\DynamicSku")
8086
ship_bundle_items: ShipBundleItemsEnum @doc(description: "Indicates whether to ship bundle items together or individually.") @resolver(class: "Magento\\BundleGraphQl\\Model\\Resolver\\Product\\Fields\\ShipBundleItems")
8187
dynamic_weight: Boolean @doc(description: "Indicates whether the bundle product has a dynamically calculated weight.") @resolver(class: "Magento\\BundleGraphQl\\Model\\Resolver\\Product\\Fields\\DynamicWeight")
88+
price_details: PriceDetails @doc(description: "The price details of the main product") @resolver(class: "Magento\\BundleGraphQl\\Model\\Resolver\\BundlePriceDetails")
8289
items: [BundleItem] @doc(description: "An array containing information about individual bundle items.") @resolver(class: "Magento\\BundleGraphQl\\Model\\Resolver\\BundleItems")
8390
}
8491

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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\GraphQl\Bundle;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
12+
class BundleProductMainPriceTest extends GraphQlAbstract
13+
{
14+
public function getQuery()
15+
{
16+
$productSku = 'fixed_bundle_product_with_special_price';
17+
return <<<QUERY
18+
{
19+
products(filter:{ sku:{eq:"{$productSku}"}})
20+
{
21+
items {
22+
url_key
23+
sku
24+
price_range {
25+
minimum_price {
26+
final_price {
27+
value
28+
currency
29+
}
30+
discount {
31+
percent_off
32+
amount_off
33+
}
34+
regular_price {
35+
value
36+
currency
37+
} }
38+
maximum_price {
39+
final_price {
40+
value
41+
currency
42+
}
43+
regular_price {
44+
value
45+
currency
46+
}
47+
discount {
48+
percent_off
49+
amount_off
50+
}
51+
} }
52+
... on BundleProduct {
53+
price_details{
54+
main_price
55+
main_final_price
56+
discount_percentage
57+
}
58+
dynamic_sku
59+
dynamic_price
60+
dynamic_weight
61+
price_view
62+
ship_bundle_items
63+
items {
64+
uid
65+
title
66+
required
67+
type
68+
position
69+
sku
70+
options {
71+
uid
72+
quantity
73+
position
74+
is_default
75+
price
76+
price_type
77+
can_change_quantity
78+
label
79+
product {
80+
uid
81+
name
82+
sku
83+
price_range {
84+
minimum_price {
85+
final_price {
86+
value
87+
}
88+
regular_price {
89+
value
90+
} }
91+
maximum_price {
92+
final_price {
93+
value
94+
currency
95+
}
96+
regular_price {
97+
value
98+
currency
99+
}
100+
}
101+
102+
}
103+
__typename
104+
}
105+
}
106+
}
107+
}
108+
}
109+
}
110+
}
111+
QUERY;
112+
}
113+
114+
/**
115+
* @magentoApiDataFixture Magento/Bundle/_files/fixed_bundle_product_with_special_price.php
116+
* @return void
117+
*/
118+
public function testBundleProductPriceDetails(): void
119+
{
120+
$query = $this->getQuery();
121+
$response = $this->graphQlQuery($query);
122+
$product = $response['products']['items'][0];
123+
$this->assertArrayHasKey('price_details', $product);
124+
$priceDetails = $product['price_details'];
125+
$this->assertArrayHasKey('main_price', $priceDetails);
126+
$this->assertArrayHasKey('main_final_price', $priceDetails);
127+
$this->assertArrayHasKey('discount_percentage', $priceDetails);
128+
$this->assertEquals(50.0, $priceDetails['main_price']);
129+
$this->assertEquals(40.0, $priceDetails['main_final_price']);
130+
$this->assertEquals(20, $priceDetails['discount_percentage']);
131+
}
132+
}

0 commit comments

Comments
 (0)