diff --git a/app/Http/Controllers/MagentoMigrationController.php b/app/Http/Controllers/MagentoMigrationController.php index 53af063..6b2d63d 100644 --- a/app/Http/Controllers/MagentoMigrationController.php +++ b/app/Http/Controllers/MagentoMigrationController.php @@ -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, diff --git a/app/Services/MagentoCategoryMigrationService.php b/app/Services/MagentoCategoryMigrationService.php index 6169a77..8058b21 100644 --- a/app/Services/MagentoCategoryMigrationService.php +++ b/app/Services/MagentoCategoryMigrationService.php @@ -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' => [] ]; } diff --git a/resources/views/migration/index.blade.php b/resources/views/migration/index.blade.php index f34a6bc..803aca8 100644 --- a/resources/views/migration/index.blade.php +++ b/resources/views/migration/index.blade.php @@ -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'}`;