Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReindexRuleProductPrice.php
Go to the documentation of this file.
1 <?php
8 
13 {
17  private $storeManager;
18 
22  private $ruleProductsSelectBuilder;
23 
27  private $productPriceCalculator;
28 
32  private $dateTime;
33 
37  private $pricesPersistor;
38 
46  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
52  ) {
53  $this->storeManager = $storeManager;
54  $this->ruleProductsSelectBuilder = $ruleProductsSelectBuilder;
55  $this->productPriceCalculator = $productPriceCalculator;
56  $this->dateTime = $dateTime;
57  $this->pricesPersistor = $pricesPersistor;
58  }
59 
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 
81  foreach ($this->storeManager->getWebsites() as $website) {
82  $productsStmt = $this->ruleProductsSelectBuilder->build($website->getId(), $product, $useAdditionalTable);
83  $dayPrices = [];
84  $stopFlags = [];
85  $prevKey = null;
86 
87  while ($ruleData = $productsStmt->fetch()) {
88  $ruleProductId = $ruleData['product_id'];
89  $productKey = $ruleProductId .
90  '_' .
91  $ruleData['website_id'] .
92  '_' .
93  $ruleData['customer_group_id'];
94 
95  if ($prevKey && $prevKey != $productKey) {
96  $stopFlags = [];
97  if (count($dayPrices) > $batchCount) {
98  $this->pricesPersistor->execute($dayPrices, $useAdditionalTable);
99  $dayPrices = [];
100  }
101  }
102 
103  $ruleData['from_time'] = $this->roundTime($ruleData['from_time']);
104  $ruleData['to_time'] = $this->roundTime($ruleData['to_time']);
108  for ($time = $fromDate; $time <= $toDate; $time += IndexBuilder::SECONDS_IN_DAY) {
109  if (($ruleData['from_time'] == 0 ||
110  $time >= $ruleData['from_time']) && ($ruleData['to_time'] == 0 ||
111  $time <= $ruleData['to_time'])
112  ) {
113  $priceKey = $time . '_' . $productKey;
114 
115  if (isset($stopFlags[$priceKey])) {
116  continue;
117  }
118 
119  if (!isset($dayPrices[$priceKey])) {
120  $dayPrices[$priceKey] = [
121  'rule_date' => $time,
122  'website_id' => $ruleData['website_id'],
123  'customer_group_id' => $ruleData['customer_group_id'],
124  'product_id' => $ruleProductId,
125  'rule_price' => $this->productPriceCalculator->calculate($ruleData),
126  'latest_start_date' => $ruleData['from_time'],
127  'earliest_end_date' => $ruleData['to_time'],
128  ];
129  } else {
130  $dayPrices[$priceKey]['rule_price'] = $this->productPriceCalculator->calculate(
131  $ruleData,
132  $dayPrices[$priceKey]
133  );
134  $dayPrices[$priceKey]['latest_start_date'] = max(
135  $dayPrices[$priceKey]['latest_start_date'],
136  $ruleData['from_time']
137  );
138  $dayPrices[$priceKey]['earliest_end_date'] = min(
139  $dayPrices[$priceKey]['earliest_end_date'],
140  $ruleData['to_time']
141  );
142  }
143 
144  if ($ruleData['action_stop']) {
145  $stopFlags[$priceKey] = true;
146  }
147  }
148  }
149 
150  $prevKey = $productKey;
151  }
152  $this->pricesPersistor->execute($dayPrices, $useAdditionalTable);
153  }
154  return true;
155  }
156 
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;
167  }
168 }
execute( $batchCount, \Magento\Catalog\Model\Product $product=null, $useAdditionalTable=false)
$storeManager
$ruleData
Definition: tax_rule.php:26
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\CatalogRule\Model\Indexer\RuleProductsSelectBuilder $ruleProductsSelectBuilder, \Magento\CatalogRule\Model\Indexer\ProductPriceCalculator $productPriceCalculator, \Magento\Framework\Stdlib\DateTime\DateTime $dateTime, \Magento\CatalogRule\Model\Indexer\RuleProductPricesPersistor $pricesPersistor)
$dateTime