6
6
7
7
namespace Magento \CatalogRule \Model \Indexer ;
8
8
9
- use Magento \Catalog \Model \Product ;
10
- use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
11
- use Magento \Store \Model \StoreManagerInterface ;
12
-
13
9
/**
14
10
* Reindex product prices according rule settings.
15
11
*/
16
12
class ReindexRuleProductPrice
17
13
{
18
14
/**
19
- * @var StoreManagerInterface
15
+ * @var \Magento\Store\Model\ StoreManagerInterface
20
16
*/
21
17
private $ storeManager ;
22
18
23
19
/**
24
- * @var RuleProductsSelectBuilder
20
+ * @var \Magento\CatalogRule\Model\Indexer\ RuleProductsSelectBuilder
25
21
*/
26
22
private $ ruleProductsSelectBuilder ;
27
23
28
24
/**
29
- * @var ProductPriceCalculator
25
+ * @var \Magento\CatalogRule\Model\Indexer\ ProductPriceCalculator
30
26
*/
31
27
private $ productPriceCalculator ;
32
28
33
29
/**
34
- * @var TimezoneInterface
30
+ * @var \Magento\Framework\Stdlib\DateTime\DateTime
35
31
*/
36
- private $ localeDate ;
32
+ private $ dateTime ;
37
33
38
34
/**
39
- * @var RuleProductPricesPersistor
35
+ * @var \Magento\CatalogRule\Model\Indexer\ RuleProductPricesPersistor
40
36
*/
41
37
private $ pricesPersistor ;
42
38
43
39
/**
44
- * @param StoreManagerInterface $storeManager
40
+ * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
41
+ */
42
+ private $ localeDate ;
43
+
44
+ /**
45
+ * @param \Magento\Store\Model\StoreManagerInterface $storeManager
45
46
* @param RuleProductsSelectBuilder $ruleProductsSelectBuilder
46
47
* @param ProductPriceCalculator $productPriceCalculator
47
- * @param TimezoneInterface $localeDate
48
- * @param RuleProductPricesPersistor $pricesPersistor
48
+ * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
49
+ * @param \Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor $pricesPersistor
50
+ * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
49
51
*/
50
52
public function __construct (
51
- StoreManagerInterface $ storeManager ,
52
- RuleProductsSelectBuilder $ ruleProductsSelectBuilder ,
53
- ProductPriceCalculator $ productPriceCalculator ,
54
- TimezoneInterface $ localeDate ,
55
- RuleProductPricesPersistor $ pricesPersistor
53
+ \Magento \Store \Model \StoreManagerInterface $ storeManager ,
54
+ \Magento \CatalogRule \Model \Indexer \RuleProductsSelectBuilder $ ruleProductsSelectBuilder ,
55
+ \Magento \CatalogRule \Model \Indexer \ProductPriceCalculator $ productPriceCalculator ,
56
+ \Magento \Framework \Stdlib \DateTime \DateTime $ dateTime ,
57
+ \Magento \CatalogRule \Model \Indexer \RuleProductPricesPersistor $ pricesPersistor ,
58
+ \Magento \Framework \Stdlib \DateTime \TimezoneInterface $ localeDate
56
59
) {
57
60
$ this ->storeManager = $ storeManager ;
58
61
$ this ->ruleProductsSelectBuilder = $ ruleProductsSelectBuilder ;
59
62
$ this ->productPriceCalculator = $ productPriceCalculator ;
60
- $ this ->localeDate = $ localeDate ;
63
+ $ this ->dateTime = $ dateTime ;
61
64
$ this ->pricesPersistor = $ pricesPersistor ;
65
+ $ this ->localeDate = $ localeDate ;
62
66
}
63
67
64
68
/**
65
69
* Reindex product prices.
66
70
*
67
71
* @param int $batchCount
68
- * @param Product|null $product
72
+ * @param \Magento\Catalog\Model\ Product|null $product
69
73
* @param bool $useAdditionalTable
70
74
* @return bool
71
75
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
72
76
*/
73
- public function execute ($ batchCount , Product $ product = null , $ useAdditionalTable = false )
74
- {
77
+ public function execute (
78
+ $ batchCount ,
79
+ \Magento \Catalog \Model \Product $ product = null ,
80
+ $ useAdditionalTable = false
81
+ ) {
82
+ $ fromDate = mktime (0 , 0 , 0 , date ('m ' ), date ('d ' ) - 1 );
83
+ $ toDate = mktime (0 , 0 , 0 , date ('m ' ), date ('d ' ) + 1 );
84
+
75
85
/**
76
86
* Update products rules prices per each website separately
77
87
* because for each website date in website's timezone should be used
@@ -81,13 +91,8 @@ public function execute($batchCount, Product $product = null, $useAdditionalTabl
81
91
$ dayPrices = [];
82
92
$ stopFlags = [];
83
93
$ prevKey = null ;
84
-
85
94
$ storeGroup = $ this ->storeManager ->getGroup ($ website ->getDefaultGroupId ());
86
- $ currentDate = $ this ->localeDate ->scopeDate ($ storeGroup ->getDefaultStoreId (), null , true );
87
- $ previousDate = (clone $ currentDate )->modify ('-1 day ' );
88
- $ previousDate ->setTime (23 , 59 , 59 );
89
- $ nextDate = (clone $ currentDate )->modify ('+1 day ' );
90
- $ nextDate ->setTime (0 , 0 , 0 );
95
+ $ storeId = $ storeGroup ->getDefaultStoreId ();
91
96
92
97
while ($ ruleData = $ productsStmt ->fetch ()) {
93
98
$ ruleProductId = $ ruleData ['product_id ' ];
@@ -105,24 +110,24 @@ public function execute($batchCount, Product $product = null, $useAdditionalTabl
105
110
}
106
111
}
107
112
113
+ $ ruleData ['from_time ' ] = $ this ->roundTime ($ ruleData ['from_time ' ]);
114
+ $ ruleData ['to_time ' ] = $ this ->roundTime ($ ruleData ['to_time ' ]);
108
115
/**
109
116
* Build prices for each day
110
117
*/
111
- foreach ([$ previousDate , $ currentDate , $ nextDate ] as $ date ) {
112
- $ time = $ date ->getTimestamp ();
118
+ for ($ time = $ fromDate ; $ time <= $ toDate ; $ time += IndexBuilder::SECONDS_IN_DAY ) {
113
119
if (($ ruleData ['from_time ' ] == 0 ||
114
120
$ time >= $ ruleData ['from_time ' ]) && ($ ruleData ['to_time ' ] == 0 ||
115
121
$ time <= $ ruleData ['to_time ' ])
116
122
) {
117
123
$ priceKey = $ time . '_ ' . $ productKey ;
118
-
119
124
if (isset ($ stopFlags [$ priceKey ])) {
120
125
continue ;
121
126
}
122
127
123
128
if (!isset ($ dayPrices [$ priceKey ])) {
124
129
$ dayPrices [$ priceKey ] = [
125
- 'rule_date ' => $ date ,
130
+ 'rule_date ' => $ this -> localeDate -> scopeDate ( $ storeId , $ time ) ,
126
131
'website_id ' => $ ruleData ['website_id ' ],
127
132
'customer_group_id ' => $ ruleData ['customer_group_id ' ],
128
133
'product_id ' => $ ruleProductId ,
@@ -155,6 +160,19 @@ public function execute($batchCount, Product $product = null, $useAdditionalTabl
155
160
}
156
161
$ this ->pricesPersistor ->execute ($ dayPrices , $ useAdditionalTable );
157
162
}
163
+
158
164
return true ;
159
165
}
166
+
167
+ /**
168
+ * @param int $timeStamp
169
+ * @return int
170
+ */
171
+ private function roundTime ($ timeStamp )
172
+ {
173
+ if (is_numeric ($ timeStamp ) && $ timeStamp != 0 ) {
174
+ $ timeStamp = $ this ->dateTime ->timestamp ($ this ->dateTime ->date ('Y-m-d 00:00:00 ' , $ timeStamp ));
175
+ }
176
+ return $ timeStamp ;
177
+ }
160
178
}
0 commit comments