Skip to content

Commit b28b649

Browse files
committed
MAGE-1180: refactored setFinalGroupPrices
1 parent 5849cd4 commit b28b649

File tree

2 files changed

+84
-55
lines changed

2 files changed

+84
-55
lines changed

Helper/Entity/Product/PriceManager/ProductWithChildren.php

Lines changed: 84 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -159,63 +159,18 @@ protected function handleZeroDefaultPrice($field, $currencyCode, $min, $max)
159159
*/
160160
protected function setFinalGroupPrices($field, $currencyCode, $min, $max, $dashedFormat, $product, $subproducts, $withTax)
161161
{
162-
if (count($subproducts) > 0) {
163-
$groupPriceList = [];
164-
$subProductsMin = self::PRICE_NOT_SET;
165-
$subProductsMax = self::PRICE_NOT_SET;
166-
/** @var Group $group */
167-
foreach ($this->groups as $group) {
168-
$groupId = (int) $group->getData('customer_group_id');
169-
$minPrice = $min;
162+
$subProductsMinArray = count($subproducts) > 0 ?
163+
$this->formatMinArray($product, $subproducts, $min, $currencyCode, $withTax) :
164+
[];
170165

171-
foreach ($subproducts as $subProduct) {
172-
$subProduct->setData('customer_group_id', $groupId);
173-
$subProduct->setData('website_id', $subProduct->getStore()->getWebsiteId());
174-
$specialPrice = $this->getSpecialPrice($subProduct, $currencyCode, $withTax, []);
175-
$tierPrice = $this->getTierPrice($subProduct, $currencyCode, $withTax);
176-
$price = $this->getTaxPrice($product, $subProduct->getPriceModel()->getFinalPrice(1, $subProduct), $withTax);
177-
178-
if (!empty($tierPrice[$groupId]) && $specialPrice[$groupId] > $tierPrice[$groupId]) {
179-
$minPrice = $tierPrice[$groupId];
180-
}
181-
182-
if ($subProductsMin === self::PRICE_NOT_SET || $price < $subProductsMin) {
183-
$subProductsMin = $price;
184-
}
166+
foreach ($this->groups as $group) {
167+
$groupId = (int) $group->getData('customer_group_id');
185168

186-
if ($subProductsMax === self::PRICE_NOT_SET || $price > $subProductsMax) {
187-
$subProductsMax = $price;
188-
}
189-
190-
$groupPriceList[$groupId]['min'] = min($minPrice, $subProductsMin);
191-
$groupPriceList[$groupId]['max'] = $subProductsMax;
192-
$subProduct->setData('customer_group_id', null);
193-
}
194-
195-
$subProductsMin = self::PRICE_NOT_SET;
196-
$subProductsMax = self::PRICE_NOT_SET;
197-
}
198-
199-
$minArray = [];
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-
/** @var Group $group */
209-
foreach ($this->groups as $group) {
210-
$groupId = (int) $group->getData('customer_group_id');
211-
$this->customData[$field][$currencyCode]['group_' . $groupId] = $minArray[$groupId]['price'];
212-
$this->customData[$field][$currencyCode]['group_' . $groupId . '_formated'] = $minArray[$groupId]['formatted'];
213-
$this->customData[$field][$currencyCode]['group_' . $groupId . '_max'] = $minArray[$groupId]['price_max'];
214-
}
215-
} else {
216-
/** @var Group $group */
217-
foreach ($this->groups as $group) {
218-
$groupId = (int) $group->getData('customer_group_id');
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 {
219174
if ($this->customData[$field][$currencyCode]['group_' . $groupId] == 0) {
220175
$this->customData[$field][$currencyCode]['group_' . $groupId] = $min;
221176
if ($min === $max) {
@@ -229,6 +184,80 @@ protected function setFinalGroupPrices($field, $currencyCode, $min, $max, $dashe
229184
}
230185
}
231186

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+
232261
/**
233262
* @param $min
234263
* @param $max

view/frontend/web/.DS_Store

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)