|
7 | 7 |
|
8 | 8 | namespace Magento\CatalogCustomerGraphQl\Model\Resolver;
|
9 | 9 |
|
| 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; |
10 | 17 | use Magento\Framework\GraphQl\Config\Element\Field;
|
| 18 | +use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; |
11 | 19 | use Magento\Framework\GraphQl\Query\ResolverInterface;
|
12 | 20 | use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
13 |
| -use Magento\Framework\Exception\LocalizedException; |
14 |
| -use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; |
15 | 21 | 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; |
19 | 22 | 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; |
23 | 23 |
|
24 | 24 | /**
|
25 | 25 | * Resolver for price_tiers
|
@@ -120,7 +120,7 @@ function () use ($productId, $context) {
|
120 | 120 |
|
121 | 121 | $productPrice = $this->tiers->getProductRegularPrice($productId) ?? 0.0;
|
122 | 122 | $tierPrices = $this->tiers->getProductTierPrices($productId) ?? [];
|
123 |
| - |
| 123 | + $tierPrices = $this->filterTierPrices($tierPrices); |
124 | 124 | return $this->formatProductTierPrices($tierPrices, $productPrice, $store);
|
125 | 125 | }
|
126 | 126 | );
|
@@ -158,4 +158,45 @@ private function formatProductTierPrices(array $tierPrices, float $productPrice,
|
158 | 158 | }
|
159 | 159 | return $tiers;
|
160 | 160 | }
|
| 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 | + } |
161 | 202 | }
|
0 commit comments