7
7
8
8
abstract class ProductWithChildren extends ProductWithoutChildren
9
9
{
10
+
11
+ const PRICE_NOT_SET = -1 ;
12
+
10
13
/**
11
14
* @param $product
12
15
* @param $withTax
@@ -156,45 +159,18 @@ protected function handleZeroDefaultPrice($field, $currencyCode, $min, $max)
156
159
*/
157
160
protected function setFinalGroupPrices ($ field , $ currencyCode , $ min , $ max , $ dashedFormat , $ product , $ subproducts , $ withTax )
158
161
{
159
- if (count ($ subproducts ) > 0 ) {
160
- $ groupPriceList = [];
161
- /** @var Group $group */
162
- foreach ($ this ->groups as $ group ) {
163
- $ groupId = (int ) $ group ->getData ('customer_group_id ' );
164
- $ minPrice = $ min ;
165
- foreach ($ subproducts as $ subProduct ) {
166
- $ subProduct ->setData ('customer_group_id ' , $ groupId );
167
- $ subProduct ->setData ('website_id ' , $ subProduct ->getStore ()->getWebsiteId ());
168
- $ specialPrice = $ this ->getSpecialPrice ($ subProduct , $ currencyCode , $ withTax , []);
169
- $ tierPrice = $ this ->getTierPrice ($ subProduct , $ currencyCode , $ withTax );
170
- $ price = $ this ->getTaxPrice ($ product , $ subProduct ->getPriceModel ()->getFinalPrice (1 , $ subProduct ), $ withTax );
171
- if (!empty ($ tierPrice [$ groupId ]) && $ specialPrice [$ groupId ] > $ tierPrice [$ groupId ]){
172
- $ minPrice = $ tierPrice [$ groupId ];
173
- }
174
- $ groupPriceList [$ groupId ]['min ' ] = min ($ minPrice , $ price );
175
- $ groupPriceList [$ groupId ]['max ' ] = max ($ max , $ price );
176
- $ subProduct ->setData ('customer_group_id ' , null );
177
- }
178
- }
162
+ $ subProductsMinArray = count ($ subproducts ) > 0 ?
163
+ $ this ->formatMinArray ($ product , $ subproducts , $ min , $ currencyCode , $ withTax ) :
164
+ [];
179
165
180
- $ minArray = [];
181
- foreach ($ groupPriceList as $ key => $ value ) {
182
- $ minArray [$ key ]['price ' ] = $ value ['min ' ];
183
- $ minArray [$ key ]['formatted ' ] = $ this ->formattedConfigPrice ($ value ['min ' ], $ value ['max ' ], $ currencyCode );
184
- if ($ currencyCode !== $ this ->baseCurrencyCode ) {
185
- $ minArray [$ key ]['formatted ' ] = $ this ->formattedConfigPrice ($ value ['min ' ], $ value ['max ' ], $ currencyCode );
186
- }
187
- }
188
- /** @var Group $group */
189
- foreach ($ this ->groups as $ group ) {
190
- $ groupId = (int ) $ group ->getData ('customer_group_id ' );
191
- $ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId ] = $ minArray [$ groupId ]['price ' ];
192
- $ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId . '_formated ' ] = $ minArray [$ groupId ]['formatted ' ];
193
- }
194
- } else {
195
- /** @var Group $group */
196
- foreach ($ this ->groups as $ group ) {
197
- $ groupId = (int ) $ group ->getData ('customer_group_id ' );
166
+ foreach ($ this ->groups as $ group ) {
167
+ $ groupId = (int ) $ group ->getData ('customer_group_id ' );
168
+
169
+ if (!empty ($ subProductsMinArray )) {
170
+ $ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId ] = $ subProductsMinArray [$ groupId ]['price ' ];
171
+ $ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId . '_formated ' ] = $ subProductsMinArray [$ groupId ]['formatted ' ];
172
+ $ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId . '_max ' ] = $ subProductsMinArray [$ groupId ]['price_max ' ];
173
+ } else {
198
174
if ($ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId ] == 0 ) {
199
175
$ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId ] = $ min ;
200
176
if ($ min === $ max ) {
@@ -208,6 +184,80 @@ protected function setFinalGroupPrices($field, $currencyCode, $min, $max, $dashe
208
184
}
209
185
}
210
186
187
+ /**
188
+ * @param $product
189
+ * @param $subproducts
190
+ * @param $min
191
+ * @param $currencyCode
192
+ * @param $withTax
193
+ * @return array
194
+ */
195
+ protected function formatMinArray ($ product , $ subproducts , $ min , $ currencyCode , $ withTax ): array
196
+ {
197
+ $ minArray = [];
198
+ $ groupPriceList = $ this ->getGroupPriceList ($ product , $ subproducts , $ min , $ currencyCode , $ withTax );
199
+
200
+ foreach ($ groupPriceList as $ key => $ value ) {
201
+ $ minArray [$ key ]['price ' ] = $ value ['min ' ];
202
+ $ minArray [$ key ]['price_max ' ] = $ value ['max ' ];
203
+ $ minArray [$ key ]['formatted ' ] = $ this ->formattedConfigPrice ($ value ['min ' ], $ value ['max ' ], $ currencyCode );
204
+ if ($ currencyCode !== $ this ->baseCurrencyCode ) {
205
+ $ minArray [$ key ]['formatted ' ] = $ this ->formattedConfigPrice ($ value ['min ' ], $ value ['max ' ], $ currencyCode );
206
+ }
207
+ }
208
+
209
+ return $ minArray ;
210
+ }
211
+
212
+ /**
213
+ * @param $product
214
+ * @param $subproducts
215
+ * @param $min
216
+ * @param $currencyCode
217
+ * @param $withTax
218
+ * @return array
219
+ */
220
+ protected function getGroupPriceList ($ product , $ subproducts , $ min , $ currencyCode , $ withTax ): array
221
+ {
222
+ $ groupPriceList = [];
223
+ $ subProductsMin = self ::PRICE_NOT_SET ;
224
+ $ subProductsMax = self ::PRICE_NOT_SET ;
225
+ /** @var Group $group */
226
+ foreach ($ this ->groups as $ group ) {
227
+ $ groupId = (int ) $ group ->getData ('customer_group_id ' );
228
+ $ minPrice = $ min ;
229
+
230
+ foreach ($ subproducts as $ subProduct ) {
231
+ $ subProduct ->setData ('customer_group_id ' , $ groupId );
232
+ $ subProduct ->setData ('website_id ' , $ subProduct ->getStore ()->getWebsiteId ());
233
+ $ specialPrice = $ this ->getSpecialPrice ($ subProduct , $ currencyCode , $ withTax , []);
234
+ $ tierPrice = $ this ->getTierPrice ($ subProduct , $ currencyCode , $ withTax );
235
+ $ price = $ this ->getTaxPrice ($ product , $ subProduct ->getPriceModel ()->getFinalPrice (1 , $ subProduct ), $ withTax );
236
+
237
+ if (!empty ($ tierPrice [$ groupId ]) && $ specialPrice [$ groupId ] > $ tierPrice [$ groupId ]) {
238
+ $ minPrice = $ tierPrice [$ groupId ];
239
+ }
240
+
241
+ if ($ subProductsMin === self ::PRICE_NOT_SET || $ price < $ subProductsMin ) {
242
+ $ subProductsMin = $ price ;
243
+ }
244
+
245
+ if ($ subProductsMax === self ::PRICE_NOT_SET || $ price > $ subProductsMax ) {
246
+ $ subProductsMax = $ price ;
247
+ }
248
+
249
+ $ groupPriceList [$ groupId ]['min ' ] = min ($ minPrice , $ subProductsMin );
250
+ $ groupPriceList [$ groupId ]['max ' ] = $ subProductsMax ;
251
+ $ subProduct ->setData ('customer_group_id ' , null );
252
+ }
253
+
254
+ $ subProductsMin = self ::PRICE_NOT_SET ;
255
+ $ subProductsMax = self ::PRICE_NOT_SET ;
256
+ }
257
+
258
+ return $ groupPriceList ;
259
+ }
260
+
211
261
/**
212
262
* @param $min
213
263
* @param $max
0 commit comments