Skip to content

Commit 1f08b3a

Browse files
authored
Merge pull request #1378 from algolia/update/MAGE-708
reverted tier price
2 parents 458ba05 + 9ccad65 commit 1f08b3a

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

Helper/Entity/Product/PriceManager/ProductWithoutChildren.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Catalog\Model\ProductFactory;
1010
use Magento\CatalogRule\Model\ResourceModel\Rule;
1111
use Magento\Customer\Model\Group;
12+
use Magento\Customer\Api\Data\GroupInterface;
1213
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;
1314
use Magento\Framework\Pricing\PriceCurrencyInterface;
1415
use Magento\Tax\Helper\Data as TaxHelper;
@@ -110,6 +111,7 @@ public function addPriceData($customData, Product $product, $subProducts): array
110111
$this->customData[$field][$currencyCode]['default'] = $this->priceCurrency->round($price);
111112
$this->customData[$field][$currencyCode]['default_formated'] = $this->formatPrice($price, $currencyCode);
112113
$specialPrice = $this->getSpecialPrice($product, $currencyCode, $withTax);
114+
$tierPrice = $this->getTierPrice($product, $currencyCode, $withTax);
113115
if ($this->areCustomersGroupsEnabled) {
114116
$this->addCustomerGroupsPrices($product, $currencyCode, $withTax, $field);
115117
}
@@ -118,6 +120,7 @@ public function addPriceData($customData, Product $product, $subProducts): array
118120
$this->customData[$field][$currencyCode]['special_to_date'] =
119121
(!empty($product->getSpecialToDate())) ? strtotime($product->getSpecialToDate()) : '';
120122
$this->addSpecialPrices($specialPrice, $field, $currencyCode);
123+
$this->addTierPrices($tierPrice, $field, $currencyCode);
121124
$this->addAdditionalData($product, $withTax, $subProducts, $currencyCode, $field);
122125
}
123126
}
@@ -228,6 +231,96 @@ protected function getSpecialPrice(Product $product, $currencyCode, $withTax): a
228231
}
229232
return $specialPrice;
230233
}
234+
235+
/**
236+
* @param Product $product
237+
* @param $currencyCode
238+
* @param $withTax
239+
* @return array
240+
*/
241+
protected function getTierPrice(Product $product, $currencyCode, $withTax)
242+
{
243+
$tierPrice = [];
244+
$tierPrices = [];
245+
246+
if (!is_null($product->getTierPrices())) {
247+
$product->setData('website_id', $product->getStore()->getWebsiteId());
248+
$productTierPrices = $product->getTierPrices();
249+
foreach ($productTierPrices as $productTierPrice) {
250+
if (!isset($tierPrices[$productTierPrice->getCustomerGroupId()])) {
251+
$tierPrices[$productTierPrice->getCustomerGroupId()] = $productTierPrice->getValue();
252+
253+
continue;
254+
}
255+
256+
$tierPrices[$productTierPrice->getCustomerGroupId()] = min(
257+
$tierPrices[$productTierPrice->getCustomerGroupId()],
258+
$productTierPrice->getValue()
259+
);
260+
}
261+
}
262+
263+
/** @var Group $group */
264+
foreach ($this->groups as $group) {
265+
$groupId = (int) $group->getData('customer_group_id');
266+
$tierPrice[$groupId] = false;
267+
268+
$currentTierPrice = null;
269+
if (!isset($tierPrices[$groupId]) && !isset($tierPrices[GroupInterface::CUST_GROUP_ALL])) {
270+
continue;
271+
}
272+
273+
if (isset($tierPrices[GroupInterface::CUST_GROUP_ALL])
274+
&& $tierPrices[GroupInterface::CUST_GROUP_ALL] !== []) {
275+
$currentTierPrice = $tierPrices[GroupInterface::CUST_GROUP_ALL];
276+
}
277+
278+
if (isset($tierPrices[$groupId]) && $tierPrices[$groupId] !== []) {
279+
$currentTierPrice = $currentTierPrice === null ?
280+
$tierPrices[$groupId] :
281+
min($currentTierPrice, $tierPrices[$groupId]);
282+
}
283+
284+
if ($currencyCode !== $this->baseCurrencyCode) {
285+
$currentTierPrice =
286+
$this->priceCurrency->round($this->convertPrice($currentTierPrice, $currencyCode));
287+
}
288+
$tierPrice[$groupId] = $this->getTaxPrice($product, $currentTierPrice, $withTax);
289+
}
290+
291+
return $tierPrice;
292+
}
293+
294+
/**
295+
* @param $tierPrice
296+
* @param $field
297+
* @param $currencyCode
298+
* @return void
299+
*/
300+
protected function addTierPrices($tierPrice, $field, $currencyCode)
301+
{
302+
if ($this->areCustomersGroupsEnabled) {
303+
/** @var Group $group */
304+
foreach ($this->groups as $group) {
305+
$groupId = (int) $group->getData('customer_group_id');
306+
307+
if ($tierPrice[$groupId]) {
308+
$this->customData[$field][$currencyCode]['group_' . $groupId . '_tier'] = $tierPrice[$groupId];
309+
310+
$this->customData[$field][$currencyCode]['group_' . $groupId . '_tier_formated'] =
311+
$this->formatPrice($tierPrice[$groupId], $currencyCode);
312+
}
313+
}
314+
315+
return;
316+
}
317+
318+
if ($tierPrice[0]) {
319+
$this->customData[$field][$currencyCode]['default_tier'] = $this->priceCurrency->round($tierPrice[0]);
320+
$this->customData[$field][$currencyCode]['default_tier_formated'] =
321+
$this->formatPrice($tierPrice[0], $currencyCode);
322+
}
323+
}
231324

232325
/**
233326
* @param $groupId

0 commit comments

Comments
 (0)