Skip to content

Commit 78c833b

Browse files
authored
Merge pull request #1688 from algolia/fix/MAGE-1180/customer-groups-ranges
MAGE-1180: fix configurable ranges with customer group prices
2 parents 779a24d + ce279e0 commit 78c833b

File tree

1 file changed

+88
-38
lines changed

1 file changed

+88
-38
lines changed

Helper/Entity/Product/PriceManager/ProductWithChildren.php

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
abstract class ProductWithChildren extends ProductWithoutChildren
99
{
10+
11+
const PRICE_NOT_SET = -1;
12+
1013
/**
1114
* @param $product
1215
* @param $withTax
@@ -156,45 +159,18 @@ protected function handleZeroDefaultPrice($field, $currencyCode, $min, $max)
156159
*/
157160
protected function setFinalGroupPrices($field, $currencyCode, $min, $max, $dashedFormat, $product, $subproducts, $withTax)
158161
{
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+
[];
179165

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 {
198174
if ($this->customData[$field][$currencyCode]['group_' . $groupId] == 0) {
199175
$this->customData[$field][$currencyCode]['group_' . $groupId] = $min;
200176
if ($min === $max) {
@@ -208,6 +184,80 @@ protected function setFinalGroupPrices($field, $currencyCode, $min, $max, $dashe
208184
}
209185
}
210186

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+
211261
/**
212262
* @param $min
213263
* @param $max

0 commit comments

Comments
 (0)