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