Skip to content

Commit 5972daa

Browse files
committed
WIP Scheduled price rule time zone correction
1 parent f3a160c commit 5972daa

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\CatalogRule\Model\Indexer;
88

9+
use DateTime;
910
use Magento\Catalog\Model\Product;
1011
use Magento\CatalogRule\Model\ResourceModel\Rule\Collection as RuleCollection;
1112
use Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory;
@@ -14,6 +15,8 @@
1415
use Magento\Framework\Pricing\PriceCurrencyInterface;
1516
use Magento\CatalogRule\Model\Indexer\IndexBuilder\ProductLoader;
1617
use Magento\CatalogRule\Model\Indexer\IndexerTableSwapperInterface as TableSwapper;
18+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
19+
use Magento\Store\Model\ScopeInterface;
1720

1821
/**
1922
* Catalog rule index builder
@@ -150,6 +153,11 @@ class IndexBuilder
150153
*/
151154
private $productLoader;
152155

156+
/**
157+
* @var TimezoneInterface|mixed
158+
*/
159+
private $localeDate;
160+
153161
/**
154162
* @param RuleCollectionFactory $ruleCollectionFactory
155163
* @param PriceCurrencyInterface $priceCurrency
@@ -170,6 +178,7 @@ class IndexBuilder
170178
* @param \Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher|null $activeTableSwitcher
171179
* @param ProductLoader|null $productLoader
172180
* @param TableSwapper|null $tableSwapper
181+
* @param TimezoneInterface|null $localeDate
173182
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
174183
*/
175184
public function __construct(
@@ -191,7 +200,8 @@ public function __construct(
191200
RuleProductPricesPersistor $pricesPersistor = null,
192201
\Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher $activeTableSwitcher = null,
193202
ProductLoader $productLoader = null,
194-
TableSwapper $tableSwapper = null
203+
TableSwapper $tableSwapper = null,
204+
?TimezoneInterface $localeDate = null
195205
) {
196206
$this->resource = $resource;
197207
$this->connection = $resource->getConnection();
@@ -231,6 +241,8 @@ public function __construct(
231241
);
232242
$this->tableSwapper = $tableSwapper ??
233243
ObjectManager::getInstance()->get(TableSwapper::class);
244+
$this->localeDate = $localeDate ??
245+
ObjectManager::getInstance()->get(TimezoneInterface::class);
234246
}
235247

236248
/**
@@ -389,7 +401,9 @@ protected function cleanByIds($productIds)
389401
* @param Rule $rule
390402
* @param int $productEntityId
391403
* @param array $websiteIds
404+
*
392405
* @return void
406+
* @throws \Exception
393407
*/
394408
private function assignProductToRule(Rule $rule, int $productEntityId, array $websiteIds): void
395409
{
@@ -404,16 +418,23 @@ private function assignProductToRule(Rule $rule, int $productEntityId, array $we
404418
);
405419

406420
$customerGroupIds = $rule->getCustomerGroupIds();
407-
$fromTime = strtotime($rule->getFromDate());
408-
$toTime = strtotime($rule->getToDate());
409-
$toTime = $toTime ? $toTime + self::SECONDS_IN_DAY - 1 : 0;
410421
$sortOrder = (int)$rule->getSortOrder();
411422
$actionOperator = $rule->getSimpleAction();
412423
$actionAmount = $rule->getDiscountAmount();
413424
$actionStop = $rule->getStopRulesProcessing();
414425

415426
$rows = [];
416427
foreach ($websiteIds as $websiteId) {
428+
$scopeTz = new \DateTimeZone(
429+
$this->localeDate->getConfigTimezone(ScopeInterface::SCOPE_WEBSITE, $websiteId)
430+
);
431+
$fromTime = $rule->getFromDate()
432+
? (new DateTime($rule->getFromDate(), $scopeTz))->getTimestamp()
433+
: 0;
434+
$toTime = $rule->getToDate()
435+
? (new DateTime($rule->getToDate(), $scopeTz))->getTimestamp() + IndexBuilder::SECONDS_IN_DAY - 1
436+
: 0;
437+
417438
foreach ($customerGroupIds as $customerGroupId) {
418439
$rows[] = [
419440
'rule_id' => $ruleId,

0 commit comments

Comments
 (0)