Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReindexRuleProduct.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
21  private $resource;
22 
26  private $activeTableSwitcher;
27 
31  private $tableSwapper;
32 
38  public function __construct(
39  \Magento\Framework\App\ResourceConnection $resource,
40  ActiveTableSwitcher $activeTableSwitcher,
41  TableSwapper $tableSwapper = null
42  ) {
43  $this->resource = $resource;
44  $this->activeTableSwitcher = $activeTableSwitcher;
45  $this->tableSwapper = $tableSwapper ??
46  ObjectManager::getInstance()->get(TableSwapper::class);
47  }
48 
59  public function execute(
60  \Magento\CatalogRule\Model\Rule $rule,
61  $batchCount,
62  $useAdditionalTable = false
63  ) {
64  if (!$rule->getIsActive() || empty($rule->getWebsiteIds())) {
65  return false;
66  }
67 
68  $connection = $this->resource->getConnection();
69  $websiteIds = $rule->getWebsiteIds();
70  if (!is_array($websiteIds)) {
71  $websiteIds = explode(',', $websiteIds);
72  }
73 
74  \Magento\Framework\Profiler::start('__MATCH_PRODUCTS__');
75  $productIds = $rule->getMatchingProductIds();
76  \Magento\Framework\Profiler::stop('__MATCH_PRODUCTS__');
77 
78  $indexTable = $this->resource->getTableName('catalogrule_product');
79  if ($useAdditionalTable) {
80  $indexTable = $this->resource->getTableName(
81  $this->tableSwapper->getWorkingTableName('catalogrule_product')
82  );
83  }
84 
85  $ruleId = $rule->getId();
86  $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;
90  $sortOrder = (int)$rule->getSortOrder();
91  $actionOperator = $rule->getSimpleAction();
92  $actionAmount = $rule->getDiscountAmount();
93  $actionStop = $rule->getStopRulesProcessing();
94 
95  $rows = [];
96 
97  foreach ($productIds as $productId => $validationByWebsite) {
98  foreach ($websiteIds as $websiteId) {
99  if (empty($validationByWebsite[$websiteId])) {
100  continue;
101  }
102  foreach ($customerGroupIds as $customerGroupId) {
103  $rows[] = [
104  'rule_id' => $ruleId,
105  'from_time' => $fromTime,
106  'to_time' => $toTime,
107  'website_id' => $websiteId,
108  'customer_group_id' => $customerGroupId,
109  'product_id' => $productId,
110  'action_operator' => $actionOperator,
111  'action_amount' => $actionAmount,
112  'action_stop' => $actionStop,
113  'sort_order' => $sortOrder,
114  ];
115 
116  if (count($rows) == $batchCount) {
117  $connection->insertMultiple($indexTable, $rows);
118  $rows = [];
119  }
120  }
121  }
122  }
123  if (!empty($rows)) {
124  $connection->insertMultiple($indexTable, $rows);
125  }
126  return true;
127  }
128 }
__construct(\Magento\Framework\App\ResourceConnection $resource, ActiveTableSwitcher $activeTableSwitcher, TableSwapper $tableSwapper=null)
$resource
Definition: bulk.php:12
execute(\Magento\CatalogRule\Model\Rule $rule, $batchCount, $useAdditionalTable=false)
$connection
Definition: bulk.php:13