Skip to content

Commit c6427ed

Browse files
committed
MC-18403: Pricing :: Product pricing schema
- fix tier prices
1 parent 5b0835e commit c6427ed

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed

app/code/Magento/BundleGraphQl/Model/Resolver/Product/Price/Provider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\BundleGraphQl\Model\Resolver\Product\Price;
99

1010
use Magento\Bundle\Pricing\Price\FinalPrice;
11+
use Magento\Catalog\Pricing\Price\BasePrice;
12+
use Magento\Bundle\Model\Product\Price;
1113
use Magento\Catalog\Pricing\Price\RegularPrice;
1214
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderInterface;
1315
use Magento\Framework\Pricing\Amount\AmountInterface;
@@ -55,6 +57,9 @@ public function getMaximalRegularPrice(SaleableInterface $product): AmountInterf
5557
*/
5658
public function getRegularPrice(SaleableInterface $product): AmountInterface
5759
{
60+
if ($product->getPriceType() == Price::PRICE_TYPE_FIXED) {
61+
return $product->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getAmount();
62+
}
5863
return $product->getPriceInfo()->getPrice(RegularPrice::PRICE_CODE)->getAmount();
5964
}
6065
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ function () use ($productId, $context) {
112112
$productPrice = $this->tiers->getProductRegularPrice($productId) ?? 0.0;
113113
$tierPrices = $this->tiers->getProductTierPrices($productId) ?? [];
114114

115-
116115
return $this->formatProductTierPrices($tierPrices, $productPrice, $store);
117116
}
118117
);
@@ -131,8 +130,15 @@ private function formatProductTierPrices(array $tierPrices, float $productPrice,
131130
$tiers = [];
132131

133132
foreach ($tierPrices as $tierPrice) {
133+
$percentValue = $tierPrice->getExtensionAttributes()->getPercentageValue();
134+
if ($percentValue && is_numeric($percentValue)) {
135+
$discount = $this->discount->getDiscountByPercent($productPrice, (float)$percentValue);
136+
} else {
137+
$discount = $this->discount->getDiscountByDifference($productPrice, (float)$tierPrice->getValue());
138+
}
139+
134140
$tiers[] = [
135-
"discount" => $this->discount->getPriceDiscount($productPrice, (float)$tierPrice->getValue()),
141+
"discount" => $discount,
136142
"quantity" => $tierPrice->getQty(),
137143
"final_price" => [
138144
"value" => $tierPrice->getValue(),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function getProductRegularPrice($productId): ?float
117117
}
118118
$product = $this->products[$productId];
119119
$priceProvider = $this->priceProviderPool->getProviderByProductType($product->getTypeId());
120-
return $priceProvider->getMinimalRegularPrice($product)->getValue();
120+
return $priceProvider->getRegularPrice($product)->getValue();
121121
}
122122

123123
/**
@@ -142,6 +142,7 @@ private function load(): void
142142
$productCollection = $this->collectionFactory->create();
143143
$productCollection->addFieldToFilter($productIdField, ['in' => $this->filterProductIds]);
144144
$productCollection->addAttributeToSelect('price');
145+
$productCollection->addAttributeToSelect('price_type');
145146
$productCollection->load();
146147
$productCollection->addTierPriceDataByGroupId($this->customerGroupId);
147148

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Price/Discount.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,29 @@ class Discount
2424
* @param float $finalPrice
2525
* @return array
2626
*/
27-
public function getPriceDiscount(float $regularPrice, float $finalPrice): array
27+
public function getDiscountByDifference(float $regularPrice, float $finalPrice): array
2828
{
2929
return [
3030
'amount_off' => $this->getPriceDifferenceAsValue($regularPrice, $finalPrice),
3131
'percent_off' => $this->getPriceDifferenceAsPercent($regularPrice, $finalPrice)
3232
];
3333
}
3434

35+
/**
36+
* Get formatted discount based on percent off
37+
*
38+
* @param float $regularPrice
39+
* @param float $percentOff
40+
* @return array
41+
*/
42+
public function getDiscountByPercent(float $regularPrice, float $percentOff): array
43+
{
44+
return [
45+
'amount_off' => $this->getPercentDiscountAsValue($regularPrice, $percentOff),
46+
'percent_off' => $percentOff
47+
];
48+
}
49+
3550
/**
3651
* Get value difference between two prices
3752
*
@@ -65,4 +80,19 @@ private function getPriceDifferenceAsPercent(float $regularPrice, float $finalPr
6580

6681
return round(($difference / $regularPrice) * 100, 2);
6782
}
83+
84+
/**
85+
* Get amount difference that percentOff represents
86+
*
87+
* @param float $regularPrice
88+
* @param float $percentOff
89+
* @return float
90+
*/
91+
private function getPercentDiscountAsValue(float $regularPrice, float $percentOff): float
92+
{
93+
$percentDecimal = $percentOff / 100;
94+
$valueDiscount = $regularPrice * $percentDecimal;
95+
96+
return round($valueDiscount, 2);
97+
}
6898
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private function formatPrice(float $regularPrice, float $finalPrice, StoreInterf
119119
'value' => $finalPrice,
120120
'currency' => $store->getCurrentCurrencyCode()
121121
],
122-
'discount' => $this->discount->getPriceDiscount($regularPrice, $finalPrice),
122+
'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice),
123123
];
124124
}
125125
}

app/code/Magento/CatalogGraphQl/Test/Unit/Model/Resolver/Product/Price/DiscountTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function setUp()
3131
*/
3232
public function testGetPriceDiscount($regularPrice, $finalPrice, $expectedAmountOff, $expectedPercentOff)
3333
{
34-
$discountResult = $this->discount->getPriceDiscount($regularPrice, $finalPrice);
34+
$discountResult = $this->discount->getDiscountByDifference($regularPrice, $finalPrice);
3535

3636
$this->assertEquals($expectedAmountOff, $discountResult['amount_off']);
3737
$this->assertEquals($expectedPercentOff, $discountResult['percent_off']);

0 commit comments

Comments
 (0)