8
8
9
9
use Magento \CatalogRule \Model \Indexer \IndexerTableSwapperInterface as TableSwapper ;
10
10
use Magento \Catalog \Model \ResourceModel \Indexer \ActiveTableSwitcher ;
11
- use Magento \Framework \App \ObjectManager ;
11
+ use Magento \CatalogRule \Model \Rule ;
12
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
13
+ use Magento \Framework \App \ResourceConnection ;
14
+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
15
+ use Magento \Store \Model \ScopeInterface ;
12
16
13
17
/**
14
18
* Reindex rule relations with products.
15
19
*/
16
20
class ReindexRuleProduct
17
21
{
18
22
/**
19
- * @var \Magento\Framework\App\ ResourceConnection
23
+ * @var ResourceConnection
20
24
*/
21
25
private $ resource ;
22
26
@@ -31,33 +35,48 @@ class ReindexRuleProduct
31
35
private $ tableSwapper ;
32
36
33
37
/**
34
- * @param \Magento\Framework\App\ResourceConnection $resource
38
+ * @var ScopeConfigInterface
39
+ */
40
+ private $ scopeConfig ;
41
+
42
+ /**
43
+ * @var TimezoneInterface
44
+ */
45
+ private $ localeDate ;
46
+
47
+ /**
48
+ * @param ResourceConnection $resource
35
49
* @param ActiveTableSwitcher $activeTableSwitcher
36
- * @param TableSwapper|null $tableSwapper
50
+ * @param TableSwapper $tableSwapper
51
+ * @param ScopeConfigInterface $scopeConfig
52
+ * @param TimezoneInterface $localeDate
37
53
*/
38
54
public function __construct (
39
- \ Magento \ Framework \ App \ ResourceConnection $ resource ,
55
+ ResourceConnection $ resource ,
40
56
ActiveTableSwitcher $ activeTableSwitcher ,
41
- TableSwapper $ tableSwapper = null
57
+ TableSwapper $ tableSwapper ,
58
+ ScopeConfigInterface $ scopeConfig ,
59
+ TimezoneInterface $ localeDate
42
60
) {
43
61
$ this ->resource = $ resource ;
44
62
$ this ->activeTableSwitcher = $ activeTableSwitcher ;
45
- $ this ->tableSwapper = $ tableSwapper ??
46
- ObjectManager::getInstance ()->get (TableSwapper::class);
63
+ $ this ->tableSwapper = $ tableSwapper ;
64
+ $ this ->scopeConfig = $ scopeConfig ;
65
+ $ this ->localeDate = $ localeDate ;
47
66
}
48
67
49
68
/**
50
69
* Reindex information about rule relations with products.
51
70
*
52
- * @param \Magento\CatalogRule\Model\ Rule $rule
71
+ * @param Rule $rule
53
72
* @param int $batchCount
54
73
* @param bool $useAdditionalTable
55
74
* @return bool
56
75
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
57
76
* @SuppressWarnings(PHPMD.NPathComplexity)
58
77
*/
59
78
public function execute (
60
- \ Magento \ CatalogRule \ Model \ Rule $ rule ,
79
+ Rule $ rule ,
61
80
$ batchCount ,
62
81
$ useAdditionalTable = false
63
82
) {
@@ -84,9 +103,6 @@ public function execute(
84
103
85
104
$ ruleId = $ rule ->getId ();
86
105
$ customerGroupIds = $ rule ->getCustomerGroupIds ();
87
- $ fromTime = strtotime ($ rule ->getFromDate ());
88
- $ toTime = strtotime ($ rule ->getToDate ());
89
- $ toTime = $ toTime ? $ toTime + \Magento \CatalogRule \Model \Indexer \IndexBuilder::SECONDS_IN_DAY - 1 : 0 ;
90
106
$ sortOrder = (int )$ rule ->getSortOrder ();
91
107
$ actionOperator = $ rule ->getSimpleAction ();
92
108
$ actionAmount = $ rule ->getDiscountAmount ();
@@ -99,6 +115,16 @@ public function execute(
99
115
if (empty ($ validationByWebsite [$ websiteId ])) {
100
116
continue ;
101
117
}
118
+
119
+ $ scopeTzPath = $ this ->localeDate ->getDefaultTimezonePath ();
120
+ $ scopeTz = new \DateTimeZone (
121
+ $ this ->scopeConfig ->getValue ($ scopeTzPath , ScopeInterface::SCOPE_WEBSITE , $ websiteId )
122
+ );
123
+ $ fromTime = (new \DateTime ($ rule ->getFromDate (), $ scopeTz ))->getTimestamp ();
124
+ $ toTime = $ rule ->getToDate ()
125
+ ? (new \DateTime ($ rule ->getToDate (), $ scopeTz ))->getTimestamp () + IndexBuilder::SECONDS_IN_DAY - 1
126
+ : 0 ;
127
+
102
128
foreach ($ customerGroupIds as $ customerGroupId ) {
103
129
$ rows [] = [
104
130
'rule_id ' => $ ruleId ,
@@ -123,6 +149,7 @@ public function execute(
123
149
if (!empty ($ rows )) {
124
150
$ connection ->insertMultiple ($ indexTable , $ rows );
125
151
}
152
+
126
153
return true ;
127
154
}
128
155
}
0 commit comments