Skip to content

Commit 9ac97b8

Browse files
Merge remote-tracking branch 'origin/MC-34265' into 2.4-develop-pr36
2 parents 1b5069e + 16bb99b commit 9ac97b8

File tree

3 files changed

+120
-5
lines changed

3 files changed

+120
-5
lines changed

app/code/Magento/CatalogRule/Cron/DailyCatalogUpdate.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,34 @@
66

77
namespace Magento\CatalogRule\Cron;
88

9+
use Magento\CatalogRule\Model\Indexer\PartialIndex;
10+
use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor;
11+
12+
/**
13+
* Daily update catalog price rule by cron
14+
*/
915
class DailyCatalogUpdate
1016
{
1117
/**
12-
* @var \Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor
18+
* @var RuleProductProcessor
1319
*/
1420
protected $ruleProductProcessor;
1521

1622
/**
17-
* @param \Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor $ruleProductProcessor
23+
* @var PartialIndex
1824
*/
19-
public function __construct(\Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor $ruleProductProcessor)
20-
{
25+
private $partialIndex;
26+
27+
/**
28+
* @param RuleProductProcessor $ruleProductProcessor
29+
* @param PartialIndex $partialIndex
30+
*/
31+
public function __construct(
32+
RuleProductProcessor $ruleProductProcessor,
33+
PartialIndex $partialIndex
34+
) {
2135
$this->ruleProductProcessor = $ruleProductProcessor;
36+
$this->partialIndex = $partialIndex;
2237
}
2338

2439
/**
@@ -31,6 +46,8 @@ public function __construct(\Magento\CatalogRule\Model\Indexer\Rule\RuleProductP
3146
*/
3247
public function execute()
3348
{
34-
$this->ruleProductProcessor->markIndexerAsInvalid();
49+
$this->ruleProductProcessor->isIndexerScheduled()
50+
? $this->partialIndex->partialUpdateCatalogRuleProductPrice()
51+
: $this->ruleProductProcessor->markIndexerAsInvalid();
3552
}
3653
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogRule\Model\Indexer;
9+
10+
use Magento\Framework\DB\Adapter\AdapterInterface;
11+
use Magento\Framework\App\ResourceConnection;
12+
13+
/**
14+
* Catalog rule partial index
15+
*
16+
* This class triggers the dependent index "catalog_product_price",
17+
* and the cache is cleared only for the matched products for partial indexing.
18+
*/
19+
class PartialIndex
20+
{
21+
/**
22+
* @var ResourceConnection
23+
*/
24+
private $resource;
25+
26+
/**
27+
* @var AdapterInterface
28+
*/
29+
private $connection;
30+
31+
/**
32+
* @var IndexBuilder
33+
*/
34+
private $indexBuilder;
35+
36+
/**
37+
* @param ResourceConnection $resource
38+
* @param IndexBuilder $indexBuilder
39+
*/
40+
public function __construct(
41+
ResourceConnection $resource,
42+
IndexBuilder $indexBuilder
43+
) {
44+
$this->resource = $resource;
45+
$this->connection = $resource->getConnection();
46+
$this->indexBuilder = $indexBuilder;
47+
}
48+
49+
/**
50+
* Synchronization replica table with original table "catalogrule_product_price"
51+
*
52+
* Used replica table for correctly working MySQL trigger
53+
*
54+
* @return void
55+
*/
56+
public function partialUpdateCatalogRuleProductPrice(): void
57+
{
58+
$this->indexBuilder->reindexFull();
59+
$indexTableName = $this->resource->getTableName('catalogrule_product_price');
60+
$select = $this->connection->select()->from(
61+
['crp' => $indexTableName],
62+
'product_id'
63+
);
64+
$selectFields = $this->connection->select()->from(
65+
['crp' => $indexTableName],
66+
[
67+
'rule_date',
68+
'customer_group_id',
69+
'product_id',
70+
'rule_price',
71+
'website_id',
72+
'latest_start_date',
73+
'earliest_end_date',
74+
]
75+
);
76+
$where = ['product_id' .' NOT IN (?)' => $select];
77+
//remove products that are no longer used in indexing
78+
$this->connection->delete($this->resource->getTableName('catalogrule_product_price_replica'), $where);
79+
//add updated products to indexing
80+
$this->connection->query(
81+
$this->connection->insertFromSelect(
82+
$selectFields,
83+
$this->resource->getTableName('catalogrule_product_price_replica'),
84+
[
85+
'rule_date',
86+
'customer_group_id',
87+
'product_id',
88+
'rule_price',
89+
'website_id',
90+
'latest_start_date',
91+
'earliest_end_date',
92+
],
93+
AdapterInterface::INSERT_ON_DUPLICATE
94+
)
95+
);
96+
}
97+
}

app/code/Magento/CatalogRule/etc/mview.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<view id="catalog_product_price" class="Magento\Catalog\Model\Indexer\Product\Price" group="indexer">
2727
<subscriptions>
2828
<table name="catalogrule_product_price" entity_column="product_id" />
29+
<table name="catalogrule_product_price_replica" entity_column="product_id" />
2930
</subscriptions>
3031
</view>
3132
</config>

0 commit comments

Comments
 (0)