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
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
44
+ * @param StoreManagerInterface $storeManager
41
45
* @param RuleProductsSelectBuilder $ruleProductsSelectBuilder
42
46
* @param ProductPriceCalculator $productPriceCalculator
43
- * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
44
- * @param \Magento\CatalogRule\Model\Indexer\ RuleProductPricesPersistor $pricesPersistor
47
+ * @param TimezoneInterface $localeDate
48
+ * @param RuleProductPricesPersistor $pricesPersistor
45
49
*/
46
50
public function __construct (
47
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
48
- \ Magento \ CatalogRule \ Model \ Indexer \ RuleProductsSelectBuilder $ ruleProductsSelectBuilder ,
49
- \ Magento \ CatalogRule \ Model \ Indexer \ ProductPriceCalculator $ productPriceCalculator ,
50
- \ Magento \ Framework \ Stdlib \ DateTime \ DateTime $ dateTime ,
51
- \ Magento \ CatalogRule \ Model \ Indexer \ RuleProductPricesPersistor $ pricesPersistor
51
+ StoreManagerInterface $ storeManager ,
52
+ RuleProductsSelectBuilder $ ruleProductsSelectBuilder ,
53
+ ProductPriceCalculator $ productPriceCalculator ,
54
+ TimezoneInterface $ localeDate ,
55
+ RuleProductPricesPersistor $ pricesPersistor
52
56
) {
53
57
$ this ->storeManager = $ storeManager ;
54
58
$ this ->ruleProductsSelectBuilder = $ ruleProductsSelectBuilder ;
55
59
$ this ->productPriceCalculator = $ productPriceCalculator ;
56
- $ this ->dateTime = $ dateTime ;
60
+ $ this ->localeDate = $ localeDate ;
57
61
$ this ->pricesPersistor = $ pricesPersistor ;
58
62
}
59
63
60
64
/**
61
65
* Reindex product prices.
62
66
*
63
67
* @param int $batchCount
64
- * @param \Magento\Catalog\Model\ Product|null $product
68
+ * @param Product|null $product
65
69
* @param bool $useAdditionalTable
66
70
* @return bool
67
71
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
68
72
*/
69
- public function execute (
70
- $ batchCount ,
71
- \Magento \Catalog \Model \Product $ product = null ,
72
- $ useAdditionalTable = false
73
- ) {
74
- $ fromDate = mktime (0 , 0 , 0 , date ('m ' ), date ('d ' ) - 1 );
75
- $ toDate = mktime (0 , 0 , 0 , date ('m ' ), date ('d ' ) + 1 );
76
-
73
+ public function execute ($ batchCount , Product $ product = null , $ useAdditionalTable = false )
74
+ {
77
75
/**
78
76
* Update products rules prices per each website separately
79
- * because of max join limit in mysql
77
+ * because for each website date in website's timezone should be used
80
78
*/
81
79
foreach ($ this ->storeManager ->getWebsites () as $ website ) {
82
80
$ productsStmt = $ this ->ruleProductsSelectBuilder ->build ($ website ->getId (), $ product , $ useAdditionalTable );
83
81
$ dayPrices = [];
84
82
$ stopFlags = [];
85
83
$ prevKey = null ;
86
84
85
+ $ 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 );
91
+
87
92
while ($ ruleData = $ productsStmt ->fetch ()) {
88
93
$ ruleProductId = $ ruleData ['product_id ' ];
89
94
$ productKey = $ ruleProductId .
@@ -100,12 +105,11 @@ public function execute(
100
105
}
101
106
}
102
107
103
- $ ruleData ['from_time ' ] = $ this ->roundTime ($ ruleData ['from_time ' ]);
104
- $ ruleData ['to_time ' ] = $ this ->roundTime ($ ruleData ['to_time ' ]);
105
108
/**
106
109
* Build prices for each day
107
110
*/
108
- for ($ time = $ fromDate ; $ time <= $ toDate ; $ time += IndexBuilder::SECONDS_IN_DAY ) {
111
+ foreach ([$ previousDate , $ currentDate , $ nextDate ] as $ date ) {
112
+ $ time = $ date ->getTimestamp ();
109
113
if (($ ruleData ['from_time ' ] == 0 ||
110
114
$ time >= $ ruleData ['from_time ' ]) && ($ ruleData ['to_time ' ] == 0 ||
111
115
$ time <= $ ruleData ['to_time ' ])
@@ -118,7 +122,7 @@ public function execute(
118
122
119
123
if (!isset ($ dayPrices [$ priceKey ])) {
120
124
$ dayPrices [$ priceKey ] = [
121
- 'rule_date ' => $ time ,
125
+ 'rule_date ' => $ date ,
122
126
'website_id ' => $ ruleData ['website_id ' ],
123
127
'customer_group_id ' => $ ruleData ['customer_group_id ' ],
124
128
'product_id ' => $ ruleProductId ,
@@ -151,18 +155,7 @@ public function execute(
151
155
}
152
156
$ this ->pricesPersistor ->execute ($ dayPrices , $ useAdditionalTable );
153
157
}
154
- return true ;
155
- }
156
158
157
- /**
158
- * @param int $timeStamp
159
- * @return int
160
- */
161
- private function roundTime ($ timeStamp )
162
- {
163
- if (is_numeric ($ timeStamp ) && $ timeStamp != 0 ) {
164
- $ timeStamp = $ this ->dateTime ->timestamp ($ this ->dateTime ->date ('Y-m-d 00:00:00 ' , $ timeStamp ));
165
- }
166
- return $ timeStamp ;
159
+ return true ;
167
160
}
168
161
}
0 commit comments