-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Category product count does not include products assigned to the category itself #40295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mohaelmrabet
wants to merge
26
commits into
magento:2.4-develop
Choose a base branch
from
mohaelmrabet:fix/category-product-count-missing-self-reference-40263
base: 2.4-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−119
Open
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
a9f2d5e
Fix category product count not including products assigned to the cat…
f28ff95
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 10a5204
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 93225de
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet dcb78fe
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 58c7e59
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 15db49d
PR review fix
015bce6
Updating unit test
4109db7
Adding integration test
d518ce9
cs code
a0d86b2
cs code
df7b608
Optimize the test
801e996
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 6c3dc6f
fix static check
9e5ead3
fixing error fixture
bb6eccc
fixing error fixture
4fad9fb
fix static issue
63b9932
fix static issue
d5f27ef
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 625881e
Remove the BULK_PROCESSING_LIMIT
b298337
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 4d90a39
fix issue check
2d0bf92
Merge branch 'fix/category-product-count-missing-self-reference-40263…
5c1aaa7
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 6384f1b
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet 44e7e48
Merge branch '2.4-develop' into fix/category-product-count-missing-se…
mohaelmrabet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
290 changes: 290 additions & 0 deletions
290
app/code/Magento/Catalog/Test/Fixture/CategoryTreeWithProducts.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,290 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2025 Adobe | ||
| * All Rights Reserved. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\Catalog\Test\Fixture; | ||
|
|
||
| use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
| use Magento\Catalog\Model\CategoryFactory; | ||
| use Magento\Catalog\Model\ProductFactory; | ||
| use Magento\Catalog\Model\ResourceModel\Product as ProductResource; | ||
| use Magento\Framework\App\ResourceConnection; | ||
| use Magento\Framework\DataObject; | ||
| use Magento\Framework\DB\Adapter\AdapterInterface; | ||
| use Magento\Store\Model\StoreManagerInterface; | ||
| use Magento\TestFramework\Fixture\Api\DataMerger; | ||
| use Magento\TestFramework\Fixture\Data\ProcessorInterface; | ||
| use Magento\TestFramework\Fixture\DataFixtureInterface; | ||
|
|
||
| /** | ||
| * Generates a multi-level category tree using a configurable fanout array | ||
| * and assigns random products to leaf categories. | ||
| * | ||
| * @SuppressWarnings(PHPMD.TooManyFields) | ||
| * @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
| * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) | ||
| * @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||
| * @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
| */ | ||
| class CategoryTreeWithProducts implements DataFixtureInterface | ||
| { | ||
| private const DEFAULT_DATA = [ | ||
| 'category_identifier' => 'CategoryBulk%uniqid%', | ||
| 'category_count' => 10, | ||
| 'product_identifier' => 'ProductBulk%uniqid%', | ||
| 'product_count' => 0, | ||
| 'depth' => 1, | ||
| 'fanout' => [], | ||
| 'root_id' => null, | ||
| ]; | ||
|
|
||
| /** | ||
| * @var AdapterInterface | ||
| */ | ||
| private AdapterInterface $connection; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private string $categoryProductTable; | ||
|
|
||
| public function __construct( | ||
| private readonly ProcessorInterface $dataProcessor, | ||
| private readonly DataMerger $dataMerger, | ||
| private readonly CategoryRepositoryInterface $categoryRepository, | ||
| private readonly CategoryFactory $categoryFactory, | ||
| private readonly StoreManagerInterface $storeManager, | ||
| private readonly ProductFactory $productFactory, | ||
| private readonly ProductResource $productResource, | ||
| ResourceConnection $resource | ||
| ) { | ||
| $this->connection = $resource->getConnection(); | ||
| $this->categoryProductTable = $resource->getTableName('catalog_category_product'); | ||
| } | ||
|
|
||
| /** | ||
| * Execute fixture. | ||
| */ | ||
| public function apply(array $data = []): ?DataObject | ||
| { | ||
| $data = $this->prepareData($data); | ||
| $productIdentifier = $data['product_identifier']; | ||
| $productsCount = (int)$data['product_count']; | ||
| $categoryIdentifier = $data['category_identifier']; | ||
| $categoriesCount = (int)$data['category_count']; | ||
| $depth = (int)$data['depth']; | ||
| $fanoutInput = $data['fanout']; | ||
| $requestedRootId = $data['root_id']; | ||
|
|
||
| if ($depth < 0 || $depth > 5) { | ||
| throw new \RuntimeException("Parameter 'depth' must be between 0 and 5."); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve root_id | ||
| */ | ||
| if ($requestedRootId === null) { | ||
| $rootId = (int)$this->storeManager->getStore()->getRootCategoryId(); | ||
| } else { | ||
| try { | ||
| $root = $this->categoryRepository->get((int)$requestedRootId); | ||
| $rootId = (int)$root->getId(); | ||
| } catch (\Exception $e) { | ||
| throw new \RuntimeException("Invalid root_id '{$requestedRootId}': " . $e->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** Compute fanout */ | ||
| $fanout = $this->computeFanout($categoriesCount, $depth, $fanoutInput); | ||
|
|
||
| /** Create products */ | ||
| $products = $productsCount > 0 | ||
| ? $this->createProducts($productsCount, $productIdentifier) | ||
| : []; | ||
|
|
||
| $leafCategories = []; | ||
| $parentCategories = []; | ||
| $levelParents = []; | ||
|
|
||
| /* ---------------- LEVEL 0 ---------------- */ | ||
| $levelParents[0] = []; | ||
| foreach (range(1, $fanout[0]) as $i) { | ||
| $levelParents[0][] = $this->createCategoryNode( | ||
| "{$categoryIdentifier}_l0_{$rootId}_{$i}", | ||
| $rootId, | ||
| ($depth === 0), | ||
| $products, | ||
| $leafCategories, | ||
| $parentCategories | ||
| ); | ||
| } | ||
| if (count($fanout) > 1) { | ||
| /* ---------------- LEVELS 1 → depth ---------------- */ | ||
| for ($level = 1; $level <= $depth; $level++) { | ||
| $levelParents[$level] = []; | ||
| foreach ($levelParents[$level - 1] as $parentId) { | ||
| foreach (range(1, $fanout[$level]) as $i) { | ||
| $levelParents[$level][] = $this->createCategoryNode( | ||
| "{$categoryIdentifier}_l{$level}_{$parentId}_{$i}", | ||
| $parentId, | ||
| ($level === $depth), | ||
| $products, | ||
| $leafCategories, | ||
| $parentCategories | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return $this->finalize( | ||
| $categoriesCount, | ||
| $categoryIdentifier, | ||
| $products, | ||
| $leafCategories, | ||
| $parentCategories | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Compute fanout | ||
| */ | ||
| private function computeFanout(int $total, int $depth, array $fanout): array | ||
| { | ||
| $computed = []; | ||
| if (count($fanout) && array_sum($fanout) > $total) { | ||
| $computed[] = $total; | ||
| return $computed; | ||
| } | ||
| $levels = $depth + 1; | ||
| for ($i = 0; $i < $levels; $i++) { | ||
| if (isset($fanout[$i]) && $fanout[$i] > 0) { | ||
| $computed[$i] = (int)$fanout[$i]; | ||
| continue; | ||
| } | ||
| // AUTO distribute | ||
| $computed[$i] = max(1, (int)floor(pow($total, 1 / $levels))); | ||
| } | ||
|
|
||
| return $computed; | ||
| } | ||
|
|
||
| private function finalize( | ||
| int $categoriesCount, | ||
| string $identifier, | ||
| array $products, | ||
| array $leafCategories, | ||
| array $parentCategories | ||
| ): DataObject { | ||
|
|
||
| $total = count($parentCategories) + count($leafCategories); | ||
| $missing = max(0, $categoriesCount - $total); | ||
|
|
||
| for ($i = 1; $i <= $missing; $i++) { | ||
| $randomParentId = $parentCategories[random_int(0, count($parentCategories) - 1)]; | ||
|
|
||
| $this->createCategoryNode( | ||
| "{$identifier}_extra_{$randomParentId}_{$i}", | ||
| $randomParentId, | ||
| true, | ||
| $products, | ||
| $leafCategories, | ||
| $parentCategories | ||
| ); | ||
| } | ||
|
|
||
| return new DataObject([ | ||
| 'products' => $products, | ||
| 'leaf_categories' => $leafCategories, | ||
| 'all_categories' => array_merge($parentCategories, $leafCategories), | ||
| ]); | ||
| } | ||
|
|
||
| /** Create products */ | ||
| private function createProducts(int $count, string $prefix): array | ||
| { | ||
| $ids = []; | ||
|
|
||
| for ($i = 1; $i <= $count; $i++) { | ||
| $product = $this->productFactory->create(); | ||
| $product->setTypeId('simple') | ||
| ->setAttributeSetId(4) | ||
| ->setSku("{$prefix}_{$i}") | ||
| ->setName("Bulk Test Product {$i}") | ||
| ->setPrice(10 + $i) | ||
| ->setVisibility(4) | ||
| ->setStatus(1); | ||
|
|
||
| $this->productResource->save($product); | ||
| $ids[] = (int)$product->getId(); | ||
| } | ||
|
|
||
| return $ids; | ||
| } | ||
|
|
||
| /** Create category node */ | ||
| private function createCategoryNode( | ||
| string $name, | ||
| int $parentId, | ||
| bool $isLeaf, | ||
| array $products, | ||
| array &$leafCategories, | ||
| array &$parentCategories | ||
| ): int { | ||
|
|
||
| $cat = $this->categoryFactory->create(); | ||
| $cat->setName($name) | ||
| ->setIsActive(true) | ||
| ->setIsAnchor(1) | ||
| ->setParentId($parentId); | ||
|
|
||
| $this->categoryRepository->save($cat); | ||
|
|
||
| $id = (int)$cat->getId(); | ||
|
|
||
| if ($isLeaf && count($products)) { | ||
| $this->assignRandomProductsToLeaf($id, $products); | ||
| $leafCategories[] = $id; | ||
| } else { | ||
| $parentCategories[] = $id; | ||
| } | ||
|
|
||
| return $id; | ||
| } | ||
|
|
||
| /** Assign products to leaf category */ | ||
| private function assignRandomProductsToLeaf(int $catId, array $products): void | ||
| { | ||
| $count = random_int(1, 5); | ||
| $selected = []; | ||
|
|
||
| for ($i = 0; $i < $count; $i++) { | ||
| $selected[] = $products[random_int(0, count($products) - 1)]; | ||
| } | ||
|
|
||
| $selected = array_unique($selected); | ||
|
|
||
| $rows = []; | ||
| foreach ($selected as $pid) { | ||
| $rows[] = [ | ||
| 'category_id' => $catId, | ||
| 'product_id' => $pid, | ||
| 'position' => 0 | ||
| ]; | ||
| } | ||
|
|
||
| if ($rows) { | ||
| $this->connection->insertMultiple($this->categoryProductTable, $rows); | ||
| } | ||
| } | ||
|
|
||
| private function prepareData(array $data): array | ||
| { | ||
| return $this->dataProcessor->process( | ||
| $this, | ||
| $this->dataMerger->merge(self::DEFAULT_DATA, $data) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.