Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Index.php
Go to the documentation of this file.
1 <?php
7 
20 
27 class Index extends AbstractDb
28 {
33  protected $storeManager;
34 
39  protected $metadataPool;
40 
44  private $tableResolver;
45 
49  private $dimensionCollectionFactory;
50 
54  private $websiteId;
55 
65  public function __construct(
66  Context $context,
69  $connectionName = null,
70  TableResolver $tableResolver = null,
71  DimensionCollectionFactory $dimensionCollectionFactory = null
72  ) {
73  parent::__construct($context, $connectionName);
74  $this->storeManager = $storeManager;
75  $this->metadataPool = $metadataPool;
76  $this->tableResolver = $tableResolver ?: ObjectManager::getInstance()->get(IndexScopeResolverInterface::class);
77  $this->dimensionCollectionFactory = $dimensionCollectionFactory
78  ?: ObjectManager::getInstance()->get(DimensionCollectionFactory::class);
79  }
80 
86  protected function _construct()
87  {
88  }
89 
97  protected function _getCatalogProductPriceData($productIds = null)
98  {
99  $connection = $this->getConnection();
100  $catalogProductIndexPriceSelect = [];
101 
102  foreach ($this->dimensionCollectionFactory->create() as $dimensions) {
103  if (!isset($dimensions[WebsiteDimensionProvider::DIMENSION_NAME]) ||
104  $this->websiteId === null ||
105  $dimensions[WebsiteDimensionProvider::DIMENSION_NAME]->getValue() === $this->websiteId) {
106  $select = $connection->select()->from(
107  $this->tableResolver->resolve('catalog_product_index_price', $dimensions),
108  ['entity_id', 'customer_group_id', 'website_id', 'min_price']
109  );
110  if ($productIds) {
111  $select->where('entity_id IN (?)', $productIds);
112  }
113  $catalogProductIndexPriceSelect[] = $select;
114  }
115  }
116 
117  $catalogProductIndexPriceUnionSelect = $connection->select()->union($catalogProductIndexPriceSelect);
118 
119  $result = [];
120  foreach ($connection->fetchAll($catalogProductIndexPriceUnionSelect) as $row) {
121  $result[$row['website_id']][$row['entity_id']][$row['customer_group_id']] = round($row['min_price'], 2);
122  }
123 
124  return $result;
125  }
126 
136  {
137  $websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
138 
139  $this->websiteId = $websiteId;
140  $priceProductsIndexData = $this->_getCatalogProductPriceData($productIds);
141  $this->websiteId = null;
142 
143  if (!isset($priceProductsIndexData[$websiteId])) {
144  return [];
145  }
146 
147  return $priceProductsIndexData[$websiteId];
148  }
149 
158  public function getCategoryProductIndexData($storeId = null, $productIds = null)
159  {
160  $connection = $this->getConnection();
161 
162  $catalogCategoryProductDimension = new Dimension(\Magento\Store\Model\Store::ENTITY, $storeId);
163 
164  $catalogCategoryProductTableName = $this->tableResolver->resolve(
166  [
167  $catalogCategoryProductDimension
168  ]
169  );
170 
171  $select = $connection->select()->from(
172  [$catalogCategoryProductTableName],
173  ['category_id', 'product_id', 'position', 'store_id']
174  )->where(
175  'store_id = ?',
176  $storeId
177  );
178 
179  if ($productIds) {
180  $select->where('product_id IN (?)', $productIds);
181  }
182 
183  $result = [];
184  foreach ($connection->fetchAll($select) as $row) {
185  $result[$row['product_id']][$row['category_id']] = $row['position'];
186  }
187 
188  return $result;
189  }
190 
198  public function getMovedCategoryProductIds($categoryId)
199  {
200  $connection = $this->getConnection();
201 
202  $identifierField = $this->metadataPool->getMetadata(CategoryInterface::class)->getIdentifierField();
203 
204  $select = $connection->select()->distinct()->from(
205  ['c_p' => $this->getTable('catalog_category_product')],
206  ['product_id']
207  )->join(
208  ['c_e' => $this->getTable('catalog_category_entity')],
209  'c_p.category_id = c_e.' . $identifierField,
210  []
211  )->where(
212  $connection->quoteInto('c_e.path LIKE ?', '%/' . $categoryId . '/%')
213  )->orWhere(
214  'c_p.category_id = ?',
215  $categoryId
216  );
217 
218  return $connection->fetchCol($select);
219  }
220 }
getCategoryProductIndexData($storeId=null, $productIds=null)
Definition: Index.php:158
$connection
Definition: bulk.php:13
__construct(Context $context, StoreManagerInterface $storeManager, MetadataPool $metadataPool, $connectionName=null, TableResolver $tableResolver=null, DimensionCollectionFactory $dimensionCollectionFactory=null)
Definition: Index.php:65