Skip to content

Commit 2cfd8a0

Browse files
committed
remove redundant foreach
1 parent 4f978a4 commit 2cfd8a0

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function () use ($productId, $context) {
120120

121121
$productPrice = $this->tiers->getProductRegularPrice($productId) ?? 0.0;
122122
$tierPrices = $this->tiers->getProductTierPrices($productId) ?? [];
123-
$tierPrices = $this->filterTierPrices($tierPrices);
123+
// $tierPrices = $this->filterTierPrices($tierPrices);
124124
return $this->formatProductTierPrices($tierPrices, $productPrice, $store);
125125
}
126126
);
@@ -137,8 +137,9 @@ function () use ($productId, $context) {
137137
private function formatProductTierPrices(array $tierPrices, float $productPrice, StoreInterface $store): array
138138
{
139139
$tiers = [];
140+
$qtyCache = [];
140141

141-
foreach ($tierPrices as $tierPrice) {
142+
foreach ($tierPrices as $key => $tierPrice) {
142143
$tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue()));
143144
$percentValue = $tierPrice->getExtensionAttributes()->getPercentageValue();
144145
if ($percentValue && is_numeric($percentValue)) {
@@ -155,35 +156,39 @@ private function formatProductTierPrices(array $tierPrices, float $productPrice,
155156
"currency" => $store->getCurrentCurrencyCode()
156157
]
157158
];
159+
160+
$this->filterTierPrices($tierPrices, $key, $tierPrice, $qtyCache, $tiers);
158161
}
159162
return $tiers;
160163
}
161164

162165
/**
163-
* Select a lower price for each quantity
166+
* Filter the lowest price for each quantity
164167
*
165-
* @param ProductTierPriceInterface[] $tierPrices
166-
*
167-
* @return array
168+
* @param array $tierPrices
169+
* @param int $key
170+
* @param ProductTierPriceInterface $tierPriceItem
171+
* @param array $qtyCache
172+
* @param array $tiers
168173
*/
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 ((float)$price->getValue() < (float)$tierPrices[$priceQty]->getValue()) {
177-
unset($tierPrices[$priceQty]);
178-
$qtyCache[$priceQty] = $item;
179-
} else {
180-
unset($tierPrices[$item]);
181-
}
174+
private function filterTierPrices(
175+
array $tierPrices,
176+
int $key,
177+
ProductTierPriceInterface $tierPriceItem,
178+
array &$qtyCache,
179+
array &$tiers
180+
) {
181+
$qty = $tierPriceItem->getQty();
182+
if (isset($qtyCache[$qty])) {
183+
$priceQty = $qtyCache[$qty];
184+
if ((float)$tierPriceItem->getValue() < (float)$tierPrices[$priceQty]->getValue()) {
185+
unset($tiers[$priceQty]);
186+
$qtyCache[$priceQty] = $key;
182187
} else {
183-
$qtyCache[$qty] = $item;
188+
unset($tiers[$key]);
184189
}
190+
} else {
191+
$qtyCache[$qty] = $key;
185192
}
186-
187-
return $tierPrices;
188193
}
189194
}

0 commit comments

Comments
 (0)