Skip to content

Commit 5d7bbcf

Browse files
authored
Merge branch 'release/3.13.1' into bugfix/MAGE-561
2 parents 60e3f85 + 1a6d14d commit 5d7bbcf

File tree

9 files changed

+153
-52
lines changed

9 files changed

+153
-52
lines changed

Helper/ConfigHelper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class ConfigHelper
145145
public const ENHANCED_QUEUE_ARCHIVE = 'algoliasearch_advanced/queue/enhanced_archive';
146146
public const NUMBER_OF_ELEMENT_BY_PAGE = 'algoliasearch_advanced/queue/number_of_element_by_page';
147147
public const ARCHIVE_LOG_CLEAR_LIMIT = 'algoliasearch_advanced/queue/archive_clear_limit';
148+
public const MAX_VIRTUAL_REPLICA_COUNT = 50;
148149

149150
/**
150151
* @var Magento\Framework\App\Config\ScopeConfigInterface
@@ -1020,7 +1021,14 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10201021

10211022
$currency = $this->getCurrencyCode($storeId);
10221023
$attributesToAdd = [];
1024+
$defaultVirtualReplicaEnabled = $this->useVirtualReplica($storeId);
1025+
$virtualReplicaCount = 0;
10231026
foreach ($attrs as $key => $attr) {
1027+
if ($virtualReplicaCount < self::MAX_VIRTUAL_REPLICA_COUNT && ($defaultVirtualReplicaEnabled || (isset($attr['virtualReplica']) && $attr['virtualReplica']))){
1028+
$virtualReplica = 1;
1029+
} else {
1030+
$virtualReplica = 0;
1031+
}
10241032
$indexName = false;
10251033
$sortAttribute = false;
10261034
if ($this->isCustomerGroupsEnabled($storeId) && $attr['attribute'] === 'price') {
@@ -1044,6 +1052,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10441052
$newAttr['attribute'] = $attr['attribute'];
10451053
$newAttr['sort'] = $attr['sort'];
10461054
$newAttr['sortLabel'] = $attr['sortLabel'];
1055+
$newAttr['virtualReplica'] = $virtualReplica;
10471056
if (!array_key_exists('label', $newAttr) && array_key_exists('sortLabel', $newAttr)) {
10481057
$newAttr['label'] = $newAttr['sortLabel'];
10491058
}
@@ -1059,6 +1068,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10591068
'custom',
10601069
];
10611070
$attributesToAdd[$newAttr['sort']][] = $newAttr;
1071+
$virtualReplicaCount++;
10621072
}
10631073
} elseif ($attr['attribute'] === 'price') {
10641074
$indexName = $originalIndexName . '_' . $attr['attribute'] . '_' . 'default' . '_' . $attr['sort'];
@@ -1069,6 +1079,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10691079
}
10701080
if ($indexName && $sortAttribute) {
10711081
$attrs[$key]['name'] = $indexName;
1082+
$attrs[$key]['virtualReplica'] = $virtualReplica;
10721083
if (!array_key_exists('label', $attrs[$key]) && array_key_exists('sortLabel', $attrs[$key])) {
10731084
$attrs[$key]['label'] = $attrs[$key]['sortLabel'];
10741085
}
@@ -1083,6 +1094,7 @@ public function getSortingIndices($originalIndexName, $storeId = null, $currentC
10831094
'exact',
10841095
'custom',
10851096
];
1097+
$virtualReplicaCount++;
10861098
}
10871099
}
10881100
$attrsToReturn = [];

Helper/Entity/Product/PriceManager/Configurable.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ class Configurable extends ProductWithChildren
99
/**
1010
* @param $groupId
1111
* @param $product
12+
* @param $subProducts
1213
* @return float|int|mixed
1314
*/
14-
protected function getRulePrice($groupId, $product)
15+
protected function getRulePrice($groupId, $product, $subProducts)
1516
{
1617
$childrenPrices = [];
17-
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $typeInstance */
1818
$typeInstance = $product->getTypeInstance();
19-
$children = $typeInstance->getUsedProducts($product);
20-
foreach ($children as $child) {
19+
if (!$typeInstance instanceof \Magento\ConfigurableProduct\Model\Product\Type\Configurable) {
20+
return parent::getRulePrice($groupId, $product, $subProducts);
21+
}
22+
23+
foreach ($subProducts as $child) {
2124
$childrenPrices[] = (float) $this->rule->getRulePrice(
2225
new DateTime(),
2326
$this->store->getWebsiteId(),

Helper/Entity/Product/PriceManager/ProductWithChildren.php

100644100755
Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ protected function addAdditionalData($product, $withTax, $subProducts, $currency
2626
$this->handleOriginalPrice($field, $currencyCode, $min, $max, $minOriginal, $maxOriginal);
2727
if (!$this->customData[$field][$currencyCode]['default']) {
2828
$this->handleZeroDefaultPrice($field, $currencyCode, $min, $max);
29-
# need to rehandle specialPrice
30-
$specialPrice = $this->getSpecialPrice($product, $currencyCode, $withTax);
31-
$this->addSpecialPrices($specialPrice, $field, $currencyCode);
3229
}
3330
if ($this->areCustomersGroupsEnabled) {
3431
$this->setFinalGroupPrices($field, $currencyCode, $min, $max, $dashedFormat, $product, $subProducts, $withTax);
@@ -51,7 +48,14 @@ protected function getMinMaxPrices(Product $product, $withTax, $subProducts, $cu
5148
if (count($subProducts) > 0) {
5249
/** @var Product $subProduct */
5350
foreach ($subProducts as $subProduct) {
54-
$price = $this->getTaxPrice($product, $subProduct->getFinalPrice(), $withTax);
51+
$specialPrice = $this->getSpecialPrice($subProduct, $currencyCode, $withTax, $subProducts);
52+
$tierPrice = $this->getTierPrice($subProduct, $currencyCode, $withTax);
53+
if (!empty($tierPrice[0]) && $specialPrice[0] > $tierPrice[0]){
54+
$minPrice = $tierPrice[0];
55+
} else {
56+
$minPrice = $specialPrice[0];
57+
}
58+
$price = $minPrice ?? $this->getTaxPrice($product, $subProduct->getFinalPrice(), $withTax);
5559
$basePrice = $this->getTaxPrice($product, $subProduct->getPrice(), $withTax);
5660
$min = min($min, $price);
5761
$original = min($original, $basePrice);
@@ -153,28 +157,32 @@ protected function handleZeroDefaultPrice($field, $currencyCode, $min, $max)
153157
protected function setFinalGroupPrices($field, $currencyCode, $min, $max, $dashedFormat, $product, $subproducts, $withTax)
154158
{
155159
if (count($subproducts) > 0) {
156-
$array = [];
160+
$groupPriceList = [];
157161
/** @var Group $group */
158162
foreach ($this->groups as $group) {
159163
$groupId = (int) $group->getData('customer_group_id');
164+
$minPrice = $min;
160165
foreach ($subproducts as $subProduct) {
161166
$subProduct->setData('customer_group_id', $groupId);
162167
$subProduct->setData('website_id', $subProduct->getStore()->getWebsiteId());
168+
$specialPrice = $this->getSpecialPrice($subProduct, $currencyCode, $withTax, []);
169+
$tierPrice = $this->getTierPrice($subProduct, $currencyCode, $withTax);
163170
$price = $this->getTaxPrice($product, $subProduct->getPriceModel()->getFinalPrice(1, $subProduct), $withTax);
164-
$array[$groupId][] = $price;
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);
165176
$subProduct->setData('customer_group_id', null);
166177
}
167178
}
179+
168180
$minArray = [];
169-
foreach ($array as $key => $value) {
170-
$minArray[$key]['price'] = min($value);
171-
$price = min($value);
172-
$formattedPrice = $this->formatPrice($price, $currencyCode);
173-
$minArray[$key]['formatted'] = $formattedPrice;
181+
foreach ($groupPriceList as $key => $value) {
182+
$minArray[$key]['price'] = $value['min'];
183+
$minArray[$key]['formatted'] = $this->formattedConfigPrice($value['min'], $value['max'], $currencyCode);
174184
if ($currencyCode !== $this->baseCurrencyCode) {
175-
$min = $this->convertPrice($price, $currencyCode);
176-
$formattedPrice = $this->formatPrice($min, $currencyCode);
177-
$minArray[$key]['formatted'] = strval($formattedPrice);
185+
$minArray[$key]['formatted'] = $this->formattedConfigPrice($value['min'], $value['max'], $currencyCode);
178186
}
179187
}
180188
/** @var Group $group */
@@ -200,6 +208,20 @@ protected function setFinalGroupPrices($field, $currencyCode, $min, $max, $dashe
200208
}
201209
}
202210

211+
/**
212+
* @param $min
213+
* @param $max
214+
* @param $currencyCode
215+
* @return mixed|string
216+
*/
217+
public function formattedConfigPrice($min, $max, $currencyCode) {
218+
if ($min != $max) {
219+
return $this->getDashedPriceFormat($min, $max, $currencyCode);
220+
} else {
221+
return $this->formatPrice($min, $currencyCode);
222+
}
223+
}
224+
203225
/**
204226
* @param $field
205227
* @param $currencyCode
@@ -219,11 +241,13 @@ public function handleOriginalPrice($field, $currencyCode, $min, $max, $minOrigi
219241
$maxOriginal,
220242
$currencyCode
221243
);
244+
$this->handleGroupOrginalPriceformated($field, $currencyCode, $this->customData[$field][$currencyCode]['default_original_formated']);
222245
} else {
223246
$this->customData[$field][$currencyCode]['default_original_formated'] = $this->formatPrice(
224247
$minOriginal,
225248
$currencyCode
226249
);
250+
$this->handleGroupOrginalPriceformated($field, $currencyCode, $this->customData[$field][$currencyCode]['default_original_formated']);
227251
}
228252
}
229253
} else {
@@ -232,6 +256,25 @@ public function handleOriginalPrice($field, $currencyCode, $min, $max, $minOrigi
232256
$minOriginal,
233257
$currencyCode
234258
);
259+
$this->handleGroupOrginalPriceformated($field, $currencyCode, $this->customData[$field][$currencyCode]['default_original_formated']);
260+
}
261+
}
262+
263+
}
264+
265+
/**
266+
* @param $field
267+
* @param $currencyCode
268+
* @param $formatedPrice
269+
* @return void
270+
*/
271+
public function handleGroupOrginalPriceformated($field, $currencyCode, $formatedPrice) {
272+
if ($this->areCustomersGroupsEnabled) {
273+
/** @var Group $group */
274+
foreach ($this->groups as $group) {
275+
$groupId = (int)$group->getData('customer_group_id');
276+
$this->customData[$field][$currencyCode]['group_' . $groupId . '_original_formated'] =
277+
$formatedPrice;
235278
}
236279
}
237280
}

Helper/Entity/Product/PriceManager/ProductWithoutChildren.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Pricing\PriceCurrencyInterface;
1616
use Magento\Tax\Helper\Data as TaxHelper;
1717
use Magento\Tax\Model\Config as TaxConfig;
18+
use Magento\Catalog\Api\ScopedProductTierPriceManagementInterface;
1819

1920
abstract class ProductWithoutChildren
2021
{
@@ -51,6 +52,11 @@ abstract class ProductWithoutChildren
5152
* @var GroupExcludedWebsiteRepositoryInterface
5253
*/
5354
protected $groupExcludedWebsiteRepository;
55+
56+
/**
57+
* @var ScopedProductTierPriceManagementInterface
58+
*/
59+
private $productTierPrice;
5460

5561
protected $store;
5662
protected $baseCurrencyCode;
@@ -67,6 +73,7 @@ abstract class ProductWithoutChildren
6773
* @param TaxHelper $taxHelper
6874
* @param Rule $rule
6975
* @param ProductFactory $productloader
76+
* @param ScopedProductTierPriceManagementInterface $productTierPrice
7077
*/
7178
public function __construct(
7279
ConfigHelper $configHelper,
@@ -76,7 +83,8 @@ public function __construct(
7683
CatalogHelper $catalogHelper,
7784
TaxHelper $taxHelper,
7885
Rule $rule,
79-
ProductFactory $productloader
86+
ProductFactory $productloader,
87+
ScopedProductTierPriceManagementInterface $productTierPrice
8088
) {
8189
$this->configHelper = $configHelper;
8290
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
@@ -86,6 +94,7 @@ public function __construct(
8694
$this->taxHelper = $taxHelper;
8795
$this->rule = $rule;
8896
$this->productloader = $productloader;
97+
$this->productTierPrice = $productTierPrice;
8998
}
9099

91100
/**
@@ -131,7 +140,7 @@ public function addPriceData($customData, Product $product, $subProducts): array
131140
$price = $this->getTaxPrice($product, $price, $withTax);
132141
$this->customData[$field][$currencyCode]['default'] = $this->priceCurrency->round($price);
133142
$this->customData[$field][$currencyCode]['default_formated'] = $this->formatPrice($price, $currencyCode);
134-
$specialPrice = $this->getSpecialPrice($product, $currencyCode, $withTax);
143+
$specialPrice = $this->getSpecialPrice($product, $currencyCode, $withTax, $subProducts);
135144
$tierPrice = $this->getTierPrice($product, $currencyCode, $withTax);
136145
if ($this->areCustomersGroupsEnabled) {
137146
$this->addCustomerGroupsPrices($product, $currencyCode, $withTax, $field);
@@ -223,16 +232,17 @@ public function getTaxPrice($product, $amount, $withTax): float
223232
* @param Product $product
224233
* @param $currencyCode
225234
* @param $withTax
235+
* @param $subProducts
226236
* @return array
227237
*/
228-
protected function getSpecialPrice(Product $product, $currencyCode, $withTax): array
238+
protected function getSpecialPrice(Product $product, $currencyCode, $withTax, $subProducts): array
229239
{
230240
$specialPrice = [];
231241
/** @var Group $group */
232242
foreach ($this->groups as $group) {
233243
$groupId = (int) $group->getData('customer_group_id');
234244
$specialPrices[$groupId] = [];
235-
$specialPrices[$groupId][] = $this->getRulePrice($groupId, $product);
245+
$specialPrices[$groupId][] = $this->getRulePrice($groupId, $product, $subProducts);
236246
// The price with applied catalog rules
237247
$specialPrices[$groupId][] = $product->getFinalPrice(); // The product's special price
238248
$specialPrices[$groupId] = array_filter($specialPrices[$groupId], function ($price) {
@@ -264,7 +274,7 @@ protected function getTierPrice(Product $product, $currencyCode, $withTax)
264274
$tierPrice = [];
265275
$tierPrices = [];
266276

267-
if (!is_null($product->getTierPrices())) {
277+
if (!empty($product->getTierPrices())) {
268278
$product->setData('website_id', $product->getStore()->getWebsiteId());
269279
$productTierPrices = $product->getTierPrices();
270280
foreach ($productTierPrices as $productTierPrice) {
@@ -279,6 +289,24 @@ protected function getTierPrice(Product $product, $currencyCode, $withTax)
279289
$productTierPrice->getValue()
280290
);
281291
}
292+
} else {
293+
/** @var Group $group */
294+
foreach ($this->groups as $group) {
295+
$customerGroupId = (int) $group->getData('customer_group_id');
296+
$productTierPrices = $this->productTierPrice->getList($product->getSku(), $customerGroupId);
297+
if(!empty($productTierPrices)) {
298+
foreach ($productTierPrices as $productTierPrice) {
299+
if (!isset($tierPrices[$productTierPrice->getCustomerGroupId()])) {
300+
$tierPrices[$productTierPrice->getCustomerGroupId()] = $productTierPrice->getValue();
301+
continue;
302+
}
303+
$tierPrices[$productTierPrice->getCustomerGroupId()] = min(
304+
$tierPrices[$productTierPrice->getCustomerGroupId()],
305+
$productTierPrice->getValue()
306+
);
307+
}
308+
}
309+
}
282310
}
283311

284312
/** @var Group $group */
@@ -346,9 +374,10 @@ protected function addTierPrices($tierPrice, $field, $currencyCode)
346374
/**
347375
* @param $groupId
348376
* @param $product
377+
* @param $subProducts
349378
* @return float
350379
*/
351-
protected function getRulePrice($groupId, $product)
380+
protected function getRulePrice($groupId, $product, $subProducts)
352381
{
353382
return (float) $this->rule->getRulePrice(
354383
new DateTime(),

0 commit comments

Comments
 (0)