Skip to content

Commit 67fe58f

Browse files
authored
Merge pull request #5073 from magento-mpi/PR-2019-12-02
MPI bugfix 2.3
2 parents 2b5567b + a719d8b commit 67fe58f

File tree

15 files changed

+326
-547
lines changed

15 files changed

+326
-547
lines changed

app/code/Magento/CatalogInventory/Ui/DataProvider/Product/Form/Modifier/AdvancedInventory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ private function prepareMeta()
255255
'actionName' => 'toggleModal',
256256
],
257257
],
258+
'imports' => [
259+
'childError' => 'product_form.product_form.advanced_inventory_modal.stock_data:error',
260+
],
258261
'title' => __('Advanced Inventory'),
259262
'provider' => false,
260263
'additionalForGroup' => true,

app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,22 @@ public function __construct(
242242
*/
243243
public function reindexById($id)
244244
{
245-
$this->reindexByIds([$id]);
245+
try {
246+
$this->cleanProductIndex([$id]);
247+
248+
$products = $this->productLoader->getProducts([$id]);
249+
$activeRules = $this->getActiveRules();
250+
foreach ($products as $product) {
251+
$this->applyRules($activeRules, $product);
252+
}
253+
254+
$this->reindexRuleGroupWebsite->execute();
255+
} catch (\Exception $e) {
256+
$this->critical($e);
257+
throw new \Magento\Framework\Exception\LocalizedException(
258+
__('Catalog rule indexing failed. See details in exception log.')
259+
);
260+
}
246261
}
247262

248263
/**
@@ -275,11 +290,18 @@ protected function doReindexByIds($ids)
275290
{
276291
$this->cleanProductIndex($ids);
277292

278-
$products = $this->productLoader->getProducts($ids);
279-
$activeRules = $this->getActiveRules();
280-
foreach ($products as $product) {
281-
$this->applyRules($activeRules, $product);
293+
/** @var Rule[] $activeRules */
294+
$activeRules = $this->getActiveRules()->getItems();
295+
foreach ($activeRules as $rule) {
296+
$rule->setProductsFilter($ids);
297+
$this->reindexRuleProduct->execute($rule, $this->batchCount);
282298
}
299+
300+
foreach ($ids as $productId) {
301+
$this->cleanProductPriceIndex([$productId]);
302+
$this->reindexRuleProductPrice->execute($this->batchCount, $productId);
303+
}
304+
283305
$this->reindexRuleGroupWebsite->execute();
284306
}
285307

@@ -365,17 +387,13 @@ protected function cleanByIds($productIds)
365387
* Assign product to rule
366388
*
367389
* @param Rule $rule
368-
* @param Product $product
390+
* @param int $productEntityId
391+
* @param array $websiteIds
369392
* @return void
370393
*/
371-
private function assignProductToRule(Rule $rule, Product $product): void
394+
private function assignProductToRule(Rule $rule, int $productEntityId, array $websiteIds): void
372395
{
373-
if (!$rule->validate($product)) {
374-
return;
375-
}
376-
377396
$ruleId = (int) $rule->getId();
378-
$productEntityId = (int) $product->getId();
379397
$ruleProductTable = $this->getTable('catalogrule_product');
380398
$this->connection->delete(
381399
$ruleProductTable,
@@ -385,7 +403,6 @@ private function assignProductToRule(Rule $rule, Product $product): void
385403
]
386404
);
387405

388-
$websiteIds = array_intersect($product->getWebsiteIds(), $rule->getWebsiteIds());
389406
$customerGroupIds = $rule->getCustomerGroupIds();
390407
$fromTime = strtotime($rule->getFromDate());
391408
$toTime = strtotime($rule->getToDate());
@@ -429,12 +446,17 @@ private function assignProductToRule(Rule $rule, Product $product): void
429446
* @param Product $product
430447
* @return $this
431448
* @throws \Exception
449+
* @deprecated
450+
* @see ReindexRuleProduct::execute
432451
* @SuppressWarnings(PHPMD.NPathComplexity)
433452
*/
434453
protected function applyRule(Rule $rule, $product)
435454
{
436-
$this->assignProductToRule($rule, $product);
437-
$this->reindexRuleProductPrice->execute($this->batchCount, $product);
455+
if ($rule->validate($product)) {
456+
$websiteIds = array_intersect($product->getWebsiteIds(), $rule->getWebsiteIds());
457+
$this->assignProductToRule($rule, $product->getId(), $websiteIds);
458+
}
459+
$this->reindexRuleProductPrice->execute($this->batchCount, $product->getId());
438460
$this->reindexRuleGroupWebsite->execute();
439461

440462
return $this;
@@ -450,11 +472,16 @@ protected function applyRule(Rule $rule, $product)
450472
private function applyRules(RuleCollection $ruleCollection, Product $product): void
451473
{
452474
foreach ($ruleCollection as $rule) {
453-
$this->assignProductToRule($rule, $product);
475+
if (!$rule->validate($product)) {
476+
continue;
477+
}
478+
479+
$websiteIds = array_intersect($product->getWebsiteIds(), $rule->getWebsiteIds());
480+
$this->assignProductToRule($rule, $product->getId(), $websiteIds);
454481
}
455482

456483
$this->cleanProductPriceIndex([$product->getId()]);
457-
$this->reindexRuleProductPrice->execute($this->batchCount, $product);
484+
$this->reindexRuleProductPrice->execute($this->batchCount, $product->getId());
458485
}
459486

460487
/**
@@ -507,7 +534,7 @@ protected function updateRuleProductData(Rule $rule)
507534
*/
508535
protected function applyAllRules(Product $product = null)
509536
{
510-
$this->reindexRuleProductPrice->execute($this->batchCount, $product);
537+
$this->reindexRuleProductPrice->execute($this->batchCount, $product->getId());
511538
$this->reindexRuleGroupWebsite->execute();
512539
return $this;
513540
}
@@ -562,7 +589,7 @@ protected function calcRuleProductPrice($ruleData, $productData = null)
562589
*/
563590
protected function getRuleProductsStmt($websiteId, Product $product = null)
564591
{
565-
return $this->ruleProductsSelectBuilder->build($websiteId, $product);
592+
return $this->ruleProductsSelectBuilder->build((int) $websiteId, (int) $product->getId());
566593
}
567594

568595
/**

app/code/Magento/CatalogRule/Model/Indexer/ReindexRuleProductPrice.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Magento\CatalogRule\Model\Indexer;
88

9-
use Magento\Catalog\Model\Product;
109
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1110
use Magento\Store\Model\StoreManagerInterface;
1211

@@ -65,19 +64,19 @@ public function __construct(
6564
* Reindex product prices.
6665
*
6766
* @param int $batchCount
68-
* @param Product|null $product
67+
* @param int|null $productId
6968
* @param bool $useAdditionalTable
7069
* @return bool
7170
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
7271
*/
73-
public function execute($batchCount, Product $product = null, $useAdditionalTable = false)
72+
public function execute(int $batchCount, ?int $productId = null, bool $useAdditionalTable = false)
7473
{
7574
/**
7675
* Update products rules prices per each website separately
7776
* because for each website date in website's timezone should be used
7877
*/
7978
foreach ($this->storeManager->getWebsites() as $website) {
80-
$productsStmt = $this->ruleProductsSelectBuilder->build($website->getId(), $product, $useAdditionalTable);
79+
$productsStmt = $this->ruleProductsSelectBuilder->build($website->getId(), $productId, $useAdditionalTable);
8180
$dayPrices = [];
8281
$stopFlags = [];
8382
$prevKey = null;

app/code/Magento/CatalogRule/Model/Indexer/RuleProductsSelectBuilder.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,12 @@ public function __construct(
7474
* Build select for indexer according passed parameters.
7575
*
7676
* @param int $websiteId
77-
* @param \Magento\Catalog\Model\Product|null $product
77+
* @param int|null $productId
7878
* @param bool $useAdditionalTable
7979
* @return \Zend_Db_Statement_Interface
8080
*/
81-
public function build(
82-
$websiteId,
83-
\Magento\Catalog\Model\Product $product = null,
84-
$useAdditionalTable = false
85-
) {
81+
public function build(int $websiteId, ?int $productId = null, bool $useAdditionalTable = false)
82+
{
8683
$connection = $this->resource->getConnection();
8784
$indexTable = $this->resource->getTableName('catalogrule_product');
8885
if ($useAdditionalTable) {
@@ -107,8 +104,8 @@ public function build(
107104
['rp.website_id', 'rp.customer_group_id', 'rp.product_id', 'rp.sort_order', 'rp.rule_id']
108105
);
109106

110-
if ($product && $product->getEntityId()) {
111-
$select->where('rp.product_id=?', $product->getEntityId());
107+
if ($productId) {
108+
$select->where('rp.product_id=?', $productId);
112109
}
113110

114111
/**
@@ -159,9 +156,11 @@ public function build(
159156
sprintf($joinCondition, $tableAlias, $storeId),
160157
[]
161158
);
162-
$select->columns([
163-
'default_price' => $connection->getIfNullSql($tableAlias . '.value', 'pp_default.value'),
164-
]);
159+
$select->columns(
160+
[
161+
'default_price' => $connection->getIfNullSql($tableAlias . '.value', 'pp_default.value'),
162+
]
163+
);
165164

166165
return $connection->query($select);
167166
}

0 commit comments

Comments
 (0)