@@ -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
- $ tierPrices = $ this ->filterTierPrices ($ tierPrices );
123
+ // $tierPrices = $this->filterTierPrices($tierPrices);
124
124
return $ this ->formatProductTierPrices ($ tierPrices , $ productPrice , $ store );
125
125
}
126
126
);
@@ -137,8 +137,9 @@ function () use ($productId, $context) {
137
137
private function formatProductTierPrices (array $ tierPrices , float $ productPrice , StoreInterface $ store ): array
138
138
{
139
139
$ tiers = [];
140
+ $ qtyCache = [];
140
141
141
- foreach ($ tierPrices as $ tierPrice ) {
142
+ foreach ($ tierPrices as $ key => $ tierPrice ) {
142
143
$ tierPrice ->setValue ($ this ->priceCurrency ->convertAndRound ($ tierPrice ->getValue ()));
143
144
$ percentValue = $ tierPrice ->getExtensionAttributes ()->getPercentageValue ();
144
145
if ($ percentValue && is_numeric ($ percentValue )) {
@@ -155,35 +156,39 @@ private function formatProductTierPrices(array $tierPrices, float $productPrice,
155
156
"currency " => $ store ->getCurrentCurrencyCode ()
156
157
]
157
158
];
159
+
160
+ $ this ->filterTierPrices ($ tierPrices , $ key , $ tierPrice , $ qtyCache , $ tiers );
158
161
}
159
162
return $ tiers ;
160
163
}
161
164
162
165
/**
163
- * Select a lower price for each quantity
166
+ * Filter the lowest price for each quantity
164
167
*
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
168
173
*/
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 ;
182
187
} else {
183
- $ qtyCache [ $ qty ] = $ item ;
188
+ unset( $ tiers [ $ key ]) ;
184
189
}
190
+ } else {
191
+ $ qtyCache [$ qty ] = $ key ;
185
192
}
186
-
187
- return $ tierPrices ;
188
193
}
189
194
}
0 commit comments