Skip to content

Commit 017c44e

Browse files
committed
add filter for tier price value
1 parent 4930963 commit 017c44e

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

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

Lines changed: 50 additions & 9 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
@@ -120,7 +120,7 @@ function () use ($productId, $context) {
120120

121121
$productPrice = $this->tiers->getProductRegularPrice($productId) ?? 0.0;
122122
$tierPrices = $this->tiers->getProductTierPrices($productId) ?? [];
123-
123+
$tierPrices = $this->filterTierPrices($tierPrices);
124124
return $this->formatProductTierPrices($tierPrices, $productPrice, $store);
125125
}
126126
);
@@ -158,4 +158,45 @@ private function formatProductTierPrices(array $tierPrices, float $productPrice,
158158
}
159159
return $tiers;
160160
}
161+
162+
/**
163+
* Select a lower price for each quantity
164+
*
165+
* @param ProductTierPriceInterface[] $tierPrices
166+
*
167+
* @return array
168+
*/
169+
private function filterTierPrices(array $tierPrices): array
170+
{
171+
$qtyCache = [];
172+
foreach ($tierPrices as $item => &$price) {
173+
$qty = $price->getQty();
174+
if (isset($qtyCache[$qty])) {
175+
$priceQty = $qtyCache[$qty];
176+
if ($this->isFirstPriceBetter((float)$price->getValue(), (float)$tierPrices[$priceQty]->getValue())) {
177+
unset($tierPrices[$priceQty]);
178+
$qtyCache[$priceQty] = $item;
179+
} else {
180+
unset($tierPrices[$item]);
181+
}
182+
} else {
183+
$qtyCache[$qty] = $item;
184+
}
185+
}
186+
187+
return $tierPrices;
188+
}
189+
190+
/**
191+
* Returns true if first price is better
192+
*
193+
* @param float $firstPrice
194+
* @param float $secondPrice
195+
*
196+
* @return bool
197+
*/
198+
private function isFirstPriceBetter(float $firstPrice, float $secondPrice): bool
199+
{
200+
return $firstPrice < $secondPrice;
201+
}
161202
}

0 commit comments

Comments
 (0)