@@ -66,6 +66,16 @@ class PriceTiers implements ResolverInterface
66
66
*/
67
67
private $ priceCurrency ;
68
68
69
+ /**
70
+ * @var array
71
+ */
72
+ private $ formatAndFilterTierPrices = [];
73
+
74
+ /**
75
+ * @var array
76
+ */
77
+ private $ tierPricesQty = [];
78
+
69
79
/**
70
80
* @param ValueFactory $valueFactory
71
81
* @param TiersFactory $tiersFactory
@@ -115,17 +125,16 @@ public function resolve(
115
125
return [];
116
126
}
117
127
118
- $ productId = $ product ->getId ();
128
+ $ productId = ( int ) $ product ->getId ();
119
129
$ this ->tiers ->addProductFilter ($ productId );
120
130
121
131
return $ this ->valueFactory ->create (
122
132
function () use ($ productId , $ context ) {
123
- /** @var StoreInterface $store */
124
- $ store = $ context ->getExtensionAttributes ()->getStore ();
133
+ $ currencyCode = $ context ->getExtensionAttributes ()->getStore ()->getCurrentCurrencyCode ();
125
134
126
135
$ productPrice = $ this ->tiers ->getProductRegularPrice ($ productId ) ?? 0.0 ;
127
136
$ tierPrices = $ this ->tiers ->getProductTierPrices ($ productId ) ?? [];
128
- return $ this ->formatAndFilterTierPrices ($ tierPrices , $ productPrice , $ store );
137
+ return $ this ->formatAndFilterTierPrices ($ tierPrices , $ productPrice , $ currencyCode );
129
138
}
130
139
);
131
140
}
@@ -135,48 +144,45 @@ function () use ($productId, $context) {
135
144
*
136
145
* @param ProductTierPriceInterface[] $tierPrices
137
146
* @param float $productPrice
138
- * @param StoreInterface $store
147
+ * @param string $currencyCode
139
148
* @return array
140
149
*/
141
150
private function formatAndFilterTierPrices (
142
151
array $ tierPrices ,
143
152
float $ productPrice ,
144
- StoreInterface $ store
153
+ string $ currencyCode
145
154
): array {
146
- $ tiers = [];
147
- $ qtyCache = [];
148
155
149
156
foreach ($ tierPrices as $ key => $ tierPrice ) {
150
- $ this ->formatTierPrices ($ productPrice , $ store , $ tierPrice , $ tiers );
151
- $ this ->filterTierPrices ($ tierPrices , $ key , $ tierPrice , $ qtyCache , $ tiers );
157
+ $ tierPrice ->setValue ($ this ->priceCurrency ->convertAndRound ($ tierPrice ->getValue ()));
158
+ $ this ->formatTierPrices ($ productPrice , $ currencyCode , $ tierPrice );
159
+ $ this ->filterTierPrices ($ tierPrices , $ key , $ tierPrice );
152
160
}
153
- return $ tiers ;
161
+ return $ this -> formatAndFilterTierPrices ;
154
162
}
155
163
156
164
/**
157
165
* Format tier prices for output
158
166
*
159
167
* @param float $productPrice
160
- * @param StoreInterface $store
168
+ * @param string $currencyCode
161
169
* @param ProductTierPriceInterface $tierPrice
162
- * @param array $tiers
163
170
*/
164
- private function formatTierPrices (float $ productPrice , StoreInterface $ store , & $ tierPrice, & $ tiers )
171
+ private function formatTierPrices (float $ productPrice , string $ currencyCode , $ tierPrice )
165
172
{
166
- $ tierPrice ->setValue ($ this ->priceCurrency ->convertAndRound ($ tierPrice ->getValue ()));
167
173
$ percentValue = $ tierPrice ->getExtensionAttributes ()->getPercentageValue ();
168
174
if ($ percentValue && is_numeric ($ percentValue )) {
169
175
$ discount = $ this ->discount ->getDiscountByPercent ($ productPrice , (float )$ percentValue );
170
176
} else {
171
177
$ discount = $ this ->discount ->getDiscountByDifference ($ productPrice , (float )$ tierPrice ->getValue ());
172
178
}
173
179
174
- $ tiers [] = [
180
+ $ this -> formatAndFilterTierPrices [] = [
175
181
"discount " => $ discount ,
176
182
"quantity " => $ tierPrice ->getQty (),
177
183
"final_price " => [
178
184
"value " => $ tierPrice ->getValue (),
179
- "currency " => $ store -> getCurrentCurrencyCode ()
185
+ "currency " => $ currencyCode
180
186
]
181
187
];
182
188
}
@@ -187,27 +193,23 @@ private function formatTierPrices(float $productPrice, StoreInterface $store, &$
187
193
* @param array $tierPrices
188
194
* @param int $key
189
195
* @param ProductTierPriceInterface $tierPriceItem
190
- * @param array $qtyCache
191
- * @param array $tiers
192
196
*/
193
197
private function filterTierPrices (
194
198
array $ tierPrices ,
195
199
int $ key ,
196
- ProductTierPriceInterface $ tierPriceItem ,
197
- array &$ qtyCache ,
198
- array &$ tiers
200
+ ProductTierPriceInterface $ tierPriceItem
199
201
) {
200
202
$ qty = $ tierPriceItem ->getQty ();
201
- if (isset ($ qtyCache [$ qty ])) {
202
- $ priceQty = $ qtyCache [$ qty ];
203
+ if (isset ($ this -> tierPricesQty [$ qty ])) {
204
+ $ priceQty = $ this -> tierPricesQty [$ qty ];
203
205
if ((float )$ tierPriceItem ->getValue () < (float )$ tierPrices [$ priceQty ]->getValue ()) {
204
- unset($ tiers [$ priceQty ]);
205
- $ qtyCache [$ priceQty ] = $ key ;
206
+ unset($ this -> formatAndFilterTierPrices [$ priceQty ]);
207
+ $ this -> tierPricesQty [$ priceQty ] = $ key ;
206
208
} else {
207
- unset($ tiers [$ key ]);
209
+ unset($ this -> formatAndFilterTierPrices [$ key ]);
208
210
}
209
211
} else {
210
- $ qtyCache [$ qty ] = $ key ;
212
+ $ this -> tierPricesQty [$ qty ] = $ key ;
211
213
}
212
214
}
213
215
}
0 commit comments