Skip to content

Commit ebbbad4

Browse files
committed
Merge branch '2.3-develop' of https://github.com/magento/magento2ce into PR-2019-08-22
2 parents b09e7d2 + 099c639 commit ebbbad4

File tree

12 files changed

+209
-175
lines changed

12 files changed

+209
-175
lines changed

app/code/Magento/Catalog/Model/Product/Type/Price.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,10 @@ public function calculatePrice(
596596
) {
597597
\Magento\Framework\Profiler::start('__PRODUCT_CALCULATE_PRICE__');
598598
if ($wId instanceof Store) {
599+
$sId = $wId->getId();
599600
$wId = $wId->getWebsiteId();
601+
} else {
602+
$sId = $this->_storeManager->getWebsite($wId)->getDefaultGroup()->getDefaultStoreId();
600603
}
601604

602605
$finalPrice = $basePrice;
@@ -610,7 +613,7 @@ public function calculatePrice(
610613
);
611614

612615
if ($rulePrice === false) {
613-
$date = $this->_localeDate->date(null, null, false);
616+
$date = $this->_localeDate->scopeDate($sId);
614617
$rulePrice = $this->_ruleFactory->create()->getRulePrice($date, $wId, $gId, $productId);
615618
}
616619

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

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88

99
use Magento\CatalogRule\Model\Indexer\IndexerTableSwapperInterface as TableSwapper;
1010
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
11-
use Magento\Framework\App\ObjectManager;
11+
use Magento\CatalogRule\Model\Rule;
12+
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
14+
use Magento\Store\Model\ScopeInterface;
1215

1316
/**
1417
* Reindex rule relations with products.
1518
*/
1619
class ReindexRuleProduct
1720
{
1821
/**
19-
* @var \Magento\Framework\App\ResourceConnection
22+
* @var ResourceConnection
2023
*/
2124
private $resource;
2225

@@ -31,36 +34,40 @@ class ReindexRuleProduct
3134
private $tableSwapper;
3235

3336
/**
34-
* @param \Magento\Framework\App\ResourceConnection $resource
37+
* @var TimezoneInterface
38+
*/
39+
private $localeDate;
40+
41+
/**
42+
* @param ResourceConnection $resource
3543
* @param ActiveTableSwitcher $activeTableSwitcher
36-
* @param TableSwapper|null $tableSwapper
44+
* @param TableSwapper $tableSwapper
45+
* @param TimezoneInterface $localeDate
3746
*/
3847
public function __construct(
39-
\Magento\Framework\App\ResourceConnection $resource,
48+
ResourceConnection $resource,
4049
ActiveTableSwitcher $activeTableSwitcher,
41-
TableSwapper $tableSwapper = null
50+
TableSwapper $tableSwapper,
51+
TimezoneInterface $localeDate
4252
) {
4353
$this->resource = $resource;
4454
$this->activeTableSwitcher = $activeTableSwitcher;
45-
$this->tableSwapper = $tableSwapper ??
46-
ObjectManager::getInstance()->get(TableSwapper::class);
55+
$this->tableSwapper = $tableSwapper;
56+
$this->localeDate = $localeDate;
4757
}
4858

4959
/**
5060
* Reindex information about rule relations with products.
5161
*
52-
* @param \Magento\CatalogRule\Model\Rule $rule
62+
* @param Rule $rule
5363
* @param int $batchCount
5464
* @param bool $useAdditionalTable
5565
* @return bool
5666
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5767
* @SuppressWarnings(PHPMD.NPathComplexity)
5868
*/
59-
public function execute(
60-
\Magento\CatalogRule\Model\Rule $rule,
61-
$batchCount,
62-
$useAdditionalTable = false
63-
) {
69+
public function execute(Rule $rule, $batchCount, $useAdditionalTable = false)
70+
{
6471
if (!$rule->getIsActive() || empty($rule->getWebsiteIds())) {
6572
return false;
6673
}
@@ -84,21 +91,26 @@ public function execute(
8491

8592
$ruleId = $rule->getId();
8693
$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;
9094
$sortOrder = (int)$rule->getSortOrder();
9195
$actionOperator = $rule->getSimpleAction();
9296
$actionAmount = $rule->getDiscountAmount();
9397
$actionStop = $rule->getStopRulesProcessing();
9498

9599
$rows = [];
100+
foreach ($websiteIds as $websiteId) {
101+
$scopeTz = new \DateTimeZone(
102+
$this->localeDate->getConfigTimezone(ScopeInterface::SCOPE_WEBSITE, $websiteId)
103+
);
104+
$fromTime = (new \DateTime($rule->getFromDate(), $scopeTz))->getTimestamp();
105+
$toTime = $rule->getToDate()
106+
? (new \DateTime($rule->getToDate(), $scopeTz))->getTimestamp() + IndexBuilder::SECONDS_IN_DAY - 1
107+
: 0;
96108

97-
foreach ($productIds as $productId => $validationByWebsite) {
98-
foreach ($websiteIds as $websiteId) {
109+
foreach ($productIds as $productId => $validationByWebsite) {
99110
if (empty($validationByWebsite[$websiteId])) {
100111
continue;
101112
}
113+
102114
foreach ($customerGroupIds as $customerGroupId) {
103115
$rows[] = [
104116
'rule_id' => $ruleId,
@@ -123,6 +135,7 @@ public function execute(
123135
if (!empty($rows)) {
124136
$connection->insertMultiple($indexTable, $rows);
125137
}
138+
126139
return true;
127140
}
128141
}

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

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,84 +6,89 @@
66

77
namespace Magento\CatalogRule\Model\Indexer;
88

9+
use Magento\Catalog\Model\Product;
10+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
913
/**
1014
* Reindex product prices according rule settings.
1115
*/
1216
class ReindexRuleProductPrice
1317
{
1418
/**
15-
* @var \Magento\Store\Model\StoreManagerInterface
19+
* @var StoreManagerInterface
1620
*/
1721
private $storeManager;
1822

1923
/**
20-
* @var \Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder
24+
* @var RuleProductsSelectBuilder
2125
*/
2226
private $ruleProductsSelectBuilder;
2327

2428
/**
25-
* @var \Magento\CatalogRule\Model\Indexer\ProductPriceCalculator
29+
* @var ProductPriceCalculator
2630
*/
2731
private $productPriceCalculator;
2832

2933
/**
30-
* @var \Magento\Framework\Stdlib\DateTime\DateTime
34+
* @var TimezoneInterface
3135
*/
32-
private $dateTime;
36+
private $localeDate;
3337

3438
/**
35-
* @var \Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor
39+
* @var RuleProductPricesPersistor
3640
*/
3741
private $pricesPersistor;
3842

3943
/**
40-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
44+
* @param StoreManagerInterface $storeManager
4145
* @param RuleProductsSelectBuilder $ruleProductsSelectBuilder
4246
* @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
4549
*/
4650
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
5256
) {
5357
$this->storeManager = $storeManager;
5458
$this->ruleProductsSelectBuilder = $ruleProductsSelectBuilder;
5559
$this->productPriceCalculator = $productPriceCalculator;
56-
$this->dateTime = $dateTime;
60+
$this->localeDate = $localeDate;
5761
$this->pricesPersistor = $pricesPersistor;
5862
}
5963

6064
/**
6165
* Reindex product prices.
6266
*
6367
* @param int $batchCount
64-
* @param \Magento\Catalog\Model\Product|null $product
68+
* @param Product|null $product
6569
* @param bool $useAdditionalTable
6670
* @return bool
6771
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
6872
*/
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+
{
7775
/**
7876
* 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
8078
*/
8179
foreach ($this->storeManager->getWebsites() as $website) {
8280
$productsStmt = $this->ruleProductsSelectBuilder->build($website->getId(), $product, $useAdditionalTable);
8381
$dayPrices = [];
8482
$stopFlags = [];
8583
$prevKey = null;
8684

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+
8792
while ($ruleData = $productsStmt->fetch()) {
8893
$ruleProductId = $ruleData['product_id'];
8994
$productKey = $ruleProductId .
@@ -100,12 +105,11 @@ public function execute(
100105
}
101106
}
102107

103-
$ruleData['from_time'] = $this->roundTime($ruleData['from_time']);
104-
$ruleData['to_time'] = $this->roundTime($ruleData['to_time']);
105108
/**
106109
* Build prices for each day
107110
*/
108-
for ($time = $fromDate; $time <= $toDate; $time += IndexBuilder::SECONDS_IN_DAY) {
111+
foreach ([$previousDate, $currentDate, $nextDate] as $date) {
112+
$time = $date->getTimestamp();
109113
if (($ruleData['from_time'] == 0 ||
110114
$time >= $ruleData['from_time']) && ($ruleData['to_time'] == 0 ||
111115
$time <= $ruleData['to_time'])
@@ -118,7 +122,7 @@ public function execute(
118122

119123
if (!isset($dayPrices[$priceKey])) {
120124
$dayPrices[$priceKey] = [
121-
'rule_date' => $time,
125+
'rule_date' => $date,
122126
'website_id' => $ruleData['website_id'],
123127
'customer_group_id' => $ruleData['customer_group_id'],
124128
'product_id' => $ruleProductId,
@@ -151,18 +155,7 @@ public function execute(
151155
}
152156
$this->pricesPersistor->execute($dayPrices, $useAdditionalTable);
153157
}
154-
return true;
155-
}
156158

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;
167160
}
168161
}

app/code/Magento/CatalogRule/Model/ResourceModel/Product/CollectionProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function addPriceData(ProductCollection $productCollection, $joinColumn =
9090
),
9191
$connection->quoteInto(
9292
'catalog_rule.rule_date = ?',
93-
$this->dateTime->formatDate($this->localeDate->date(null, null, false), false)
93+
$this->dateTime->formatDate($this->localeDate->scopeDate($store->getId()), false)
9494
),
9595
]
9696
),

app/code/Magento/CatalogRule/Model/ResourceModel/Product/LinkedProductSelectBuilderByCatalogRulePrice.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ public function __construct(
8888
*/
8989
public function build($productId)
9090
{
91-
$currentDate = $this->dateTime->formatDate($this->localeDate->date(null, null, false), false);
91+
$timestamp = $this->localeDate->scopeTimeStamp($this->storeManager->getStore());
92+
$currentDate = $this->dateTime->formatDate($timestamp, false);
9293
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
9394
$productTable = $this->resource->getTableName('catalog_product_entity');
9495

app/code/Magento/CatalogRule/Observer/PrepareCatalogProductCollectionPricesObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
105105
if ($observer->getEvent()->hasDate()) {
106106
$date = new \DateTime($observer->getEvent()->getDate());
107107
} else {
108-
$date = $this->localeDate->date(null, null, false);
108+
$date = (new \DateTime())->setTimestamp($this->localeDate->scopeTimeStamp($store));
109109
}
110110

111111
$productIds = [];

app/code/Magento/CatalogRule/Observer/ProcessAdminFinalPriceObserver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public function __construct(
6565
public function execute(\Magento\Framework\Event\Observer $observer)
6666
{
6767
$product = $observer->getEvent()->getProduct();
68-
$date = $this->localeDate->date(null, null, false);
68+
$storeId = $product->getStoreId();
69+
$date = $this->localeDate->scopeDate($storeId);
6970
$key = false;
7071

7172
$ruleData = $this->coreRegistry->registry('rule_data');

app/code/Magento/CatalogRule/Observer/ProcessFrontFinalPriceObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8080
if ($observer->hasDate()) {
8181
$date = new \DateTime($observer->getEvent()->getDate());
8282
} else {
83-
$date = $this->localeDate->date(null, null, false);
83+
$date = $this->localeDate->scopeDate($storeId);
8484
}
8585

8686
if ($observer->hasWebsiteId()) {

app/code/Magento/CatalogRule/Pricing/Price/CatalogRulePrice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function getValue()
8888
$this->value = (float)$this->product->getData(self::PRICE_CODE) ?: false;
8989
} else {
9090
$this->value = $this->ruleResource->getRulePrice(
91-
$this->dateTime->date(null, null, false),
91+
$this->dateTime->scopeDate($this->storeManager->getStore()->getId()),
9292
$this->storeManager->getStore()->getWebsiteId(),
9393
$this->customerSession->getCustomerGroupId(),
9494
$this->product->getId()

0 commit comments

Comments
 (0)