|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright 2011 Adobe |
4 |
| - * All Rights Reserved. |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
5 | 5 | */
|
6 | 6 | namespace Magento\Catalog\Model\ResourceModel\Category;
|
7 | 7 |
|
@@ -337,63 +337,16 @@ public function loadProductCount($items, $countRegular = true, $countAnchor = tr
|
337 | 337 | $categoryIds = array_keys($anchor);
|
338 | 338 | $countSelect = $this->getProductsCountQuery($categoryIds, (bool)$websiteId);
|
339 | 339 | $categoryProductsCount = $this->_conn->fetchPairs($countSelect);
|
340 |
| - $countFromCategoryTable = $this->getCountFromCategoryTable($categoryIds, (int)$websiteId); |
341 |
| - |
342 | 340 | foreach ($anchor as $item) {
|
343 |
| - $productsCount = 0; |
344 |
| - if (isset($categoryProductsCount[$item->getId()])) { |
345 |
| - $productsCount = (int)$categoryProductsCount[$item->getId()]; |
346 |
| - } elseif (isset($countFromCategoryTable[$item->getId()])) { |
347 |
| - $productsCount = (int)$countFromCategoryTable[$item->getId()]; |
348 |
| - } |
| 341 | + $productsCount = isset($categoryProductsCount[$item->getId()]) |
| 342 | + ? (int)$categoryProductsCount[$item->getId()] |
| 343 | + : $this->getProductsCountFromCategoryTable($item, $websiteId); |
349 | 344 | $item->setProductCount($productsCount);
|
350 | 345 | }
|
351 | 346 | }
|
352 | 347 | return $this;
|
353 | 348 | }
|
354 | 349 |
|
355 |
| - /** |
356 |
| - * Get products number for each category with bulk query |
357 |
| - * |
358 |
| - * @param array $categoryIds |
359 |
| - * @param int $websiteId |
360 |
| - * @return array |
361 |
| - */ |
362 |
| - private function getCountFromCategoryTable( |
363 |
| - array $categoryIds, |
364 |
| - int $websiteId |
365 |
| - ) : array { |
366 |
| - $subSelect = clone $this->_conn->select(); |
367 |
| - $subSelect->from(['ce2' => $this->getTable('catalog_category_entity')], 'ce2.entity_id') |
368 |
| - ->where("ce2.path LIKE CONCAT(ce.path, '/%')"); |
369 |
| - |
370 |
| - $select = clone $this->_conn->select(); |
371 |
| - $select->from( |
372 |
| - ['ce' => $this->getTable('catalog_category_entity')], |
373 |
| - 'ce.entity_id' |
374 |
| - ); |
375 |
| - $joinCondition = new \Zend_Db_Expr("ce.entity_id=cp.category_id OR cp.category_id IN ({$subSelect})"); |
376 |
| - $select->joinLeft( |
377 |
| - ['cp' => $this->getProductTable()], |
378 |
| - $joinCondition, |
379 |
| - 'COUNT(DISTINCT cp.product_id) AS product_count' |
380 |
| - ); |
381 |
| - if ($websiteId) { |
382 |
| - $select->join( |
383 |
| - ['w' => $this->getProductWebsiteTable()], |
384 |
| - 'cp.product_id = w.product_id', |
385 |
| - [] |
386 |
| - )->where( |
387 |
| - 'w.website_id = ?', |
388 |
| - $websiteId |
389 |
| - ); |
390 |
| - } |
391 |
| - $select->where('ce.entity_id IN(?)', $categoryIds); |
392 |
| - $select->group('ce.entity_id'); |
393 |
| - |
394 |
| - return $this->_conn->fetchPairs($select); |
395 |
| - } |
396 |
| - |
397 | 350 | /**
|
398 | 351 | * Add category path filter
|
399 | 352 | *
|
@@ -566,6 +519,45 @@ public function getProductTable()
|
566 | 519 | return $this->_productTable;
|
567 | 520 | }
|
568 | 521 |
|
| 522 | + /** |
| 523 | + * Get products count using catalog_category_entity table |
| 524 | + * |
| 525 | + * @param Category $item |
| 526 | + * @param string $websiteId |
| 527 | + * @return int |
| 528 | + */ |
| 529 | + private function getProductsCountFromCategoryTable(Category $item, string $websiteId): int |
| 530 | + { |
| 531 | + $productCount = 0; |
| 532 | + |
| 533 | + if ($item->getAllChildren()) { |
| 534 | + $bind = ['entity_id' => $item->getId(), 'c_path' => $item->getPath() . '/%']; |
| 535 | + $select = $this->_conn->select(); |
| 536 | + $select->from( |
| 537 | + ['main_table' => $this->getProductTable()], |
| 538 | + new \Zend_Db_Expr('COUNT(DISTINCT main_table.product_id)') |
| 539 | + )->joinInner( |
| 540 | + ['e' => $this->getTable('catalog_category_entity')], |
| 541 | + 'main_table.category_id=e.entity_id', |
| 542 | + [] |
| 543 | + )->where( |
| 544 | + '(e.entity_id = :entity_id OR e.path LIKE :c_path)' |
| 545 | + ); |
| 546 | + if ($websiteId) { |
| 547 | + $select->join( |
| 548 | + ['w' => $this->getProductWebsiteTable()], |
| 549 | + 'main_table.product_id = w.product_id', |
| 550 | + [] |
| 551 | + )->where( |
| 552 | + 'w.website_id = ?', |
| 553 | + $websiteId |
| 554 | + ); |
| 555 | + } |
| 556 | + $productCount = (int)$this->_conn->fetchOne($select, $bind); |
| 557 | + } |
| 558 | + return $productCount; |
| 559 | + } |
| 560 | + |
569 | 561 | /**
|
570 | 562 | * Get query for retrieve count of products per category
|
571 | 563 | *
|
|
0 commit comments