added category product count

This commit is contained in:
Chris Rosenau 2025-11-08 14:12:55 -07:00
parent 9f8ca957c5
commit a45edf158f
3 changed files with 51 additions and 6 deletions

View File

@ -75,7 +75,7 @@ public function getMagento1CategoryTree()
{
try {
$categories = $this->migrationService->getMagento1Categories();
$tree = $this->migrationService->buildCategoryTreeHierarchy($categories);
$tree = $this->migrationService->buildCategoryTreeHierarchy($categories, 'm1');
return response()->json([
'success' => true,
@ -97,7 +97,7 @@ public function getMagento2CategoryTree()
{
try {
$categories = $this->migrationService->getMagento2Categories();
$tree = $this->migrationService->buildCategoryTreeHierarchy($categories);
$tree = $this->migrationService->buildCategoryTreeHierarchy($categories, 'm2');
return response()->json([
'success' => true,

View File

@ -244,24 +244,68 @@ public function getMagento2Categories($storeId = null)
}
}
/**
* Get product counts for categories
*/
protected function getCategoryProductCounts($categoryIds, $source = 'm2')
{
if (empty($categoryIds)) {
return [];
}
try {
$connection = $source === 'm1' ? $this->magento1Connection : $this->magento2Connection;
$prefix = $source === 'm1' ? $this->magento1Prefix : $this->magento2Prefix;
$tableName = $prefix . 'catalog_category_product';
// Check if table exists
try {
$productCounts = DB::connection($connection)
->table($tableName)
->whereIn('category_id', $categoryIds)
->select('category_id', DB::raw('COUNT(product_id) as product_count'))
->groupBy('category_id')
->pluck('product_count', 'category_id')
->toArray();
} catch (Exception $e) {
// Table might not exist, return empty array
Log::warning("Table {$tableName} might not exist: " . $e->getMessage());
return [];
}
return $productCounts;
} catch (Exception $e) {
Log::error("Error getting product counts: " . $e->getMessage());
return [];
}
}
/**
* Build hierarchical category tree structure
*/
public function buildCategoryTreeHierarchy($categories)
public function buildCategoryTreeHierarchy($categories, $source = 'm2')
{
$categoryMap = [];
$rootCategories = [];
// Get all category IDs
$categoryIds = $categories->pluck('entity_id')->toArray();
// Get product counts for all categories
$productCounts = $this->getCategoryProductCounts($categoryIds, $source);
// First pass: create a map of all categories with children array
foreach ($categories as $category) {
$categoryMap[$category->entity_id] = [
'id' => $category->entity_id,
$categoryId = $category->entity_id;
$categoryMap[$categoryId] = [
'id' => $categoryId,
'name' => $category->name ?? 'Unnamed Category',
'parent_id' => $category->parent_id,
'level' => $category->level ?? 0,
'position' => $category->position ?? 0,
'is_active' => $category->is_active ?? 0,
'path' => $category->path ?? '',
'product_count' => $productCounts[$categoryId] ?? 0,
'children' => []
];
}

View File

@ -835,7 +835,8 @@ function createTreeNode(node, source = 'm2') {
const labelText = document.createElement('span');
labelText.className = 'tree-label-text';
labelText.textContent = node.name || 'Unnamed Category';
const productCount = node.product_count !== undefined ? node.product_count : 0;
labelText.textContent = `${node.name || 'Unnamed Category'} (${productCount})`;
const badge = document.createElement('span');
badge.className = `tree-badge ${node.is_active ? 'active' : 'inactive'}`;