From 9f7b2840af37bf6c88828eff358c2486b4d8847d Mon Sep 17 00:00:00 2001 From: SysAdmin Date: Wed, 8 Oct 2025 18:05:18 +0100 Subject: [PATCH] Fix: Populate existing variant panels with imported ProductVariant data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Previous Approach (WRONG):** - Created a separate table section to display ProductVariants - User wanted data in the EXISTING collapsible panels, not a new section **Proper Fix:** - ProductImportService creates records in ProductVariants table - Edit page's collapsible panels read from VariantsJson field (different system) - Solution: Convert ProductVariants → VariantsJson format on page load **Changes:** 1. **ProductsController.cs (Lines 105-115):** - Load ProductVariants from database - If VariantsJson empty but ProductVariants exist, convert them - Format: `[{Weight: "28g", Price: 700, StockQty: 100}, ...]` - JavaScript reads Price and StockQty to populate collapsible panel fields 2. **Edit.cshtml:** - Removed the extra table section - Existing collapsible panels now display imported data automatically **Result:** When you open Edit page, expand "Product Variants" → "Price, Stock & Weight Details", the fields will be PRE-FILLED with your imported values (700, 100, etc.) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../Admin/Controllers/ProductsController.cs | 16 +++- .../Areas/Admin/Views/Products/Edit.cshtml | 95 ------------------- 2 files changed, 14 insertions(+), 97 deletions(-) diff --git a/LittleShop/Areas/Admin/Controllers/ProductsController.cs b/LittleShop/Areas/Admin/Controllers/ProductsController.cs index d821c90..7af0e61 100644 --- a/LittleShop/Areas/Admin/Controllers/ProductsController.cs +++ b/LittleShop/Areas/Admin/Controllers/ProductsController.cs @@ -98,10 +98,22 @@ public class ProductsController : Controller var productMultiBuys = await _productService.GetProductMultiBuysAsync(id); ViewData["ProductMultiBuys"] = productMultiBuys; - // Load product variants + // Load product variants and convert to VariantsJson format for UI var productVariants = await _productService.GetProductVariantsAsync(id); ViewData["ProductVariants"] = productVariants; + // If product has no VariantsJson but has ProductVariant records, convert them + if ((string.IsNullOrEmpty(product.VariantsJson) || product.VariantsJson == "[]") && productVariants.Any()) + { + var variantsForJson = productVariants.Select(v => new + { + Weight = v.Name, // Use variant name as the "Weight" property + Price = v.Price, + StockQty = v.StockLevel + }).ToList(); + product.VariantsJson = System.Text.Json.JsonSerializer.Serialize(variantsForJson); + } + // TODO: Add ReviewService injection and retrieve actual reviews // For now, providing mock review data for demonstration ViewData["ProductReviews"] = new[] @@ -139,7 +151,7 @@ public class ProductsController : Controller StockQuantity = product.StockQuantity, CategoryId = product.CategoryId, VariantCollectionId = product.VariantCollectionId, - VariantsJson = product.VariantsJson, + VariantsJson = product.VariantsJson, // Now contains converted ProductVariants data IsActive = product.IsActive }; diff --git a/LittleShop/Areas/Admin/Views/Products/Edit.cshtml b/LittleShop/Areas/Admin/Views/Products/Edit.cshtml index 3744982..697273e 100644 --- a/LittleShop/Areas/Admin/Views/Products/Edit.cshtml +++ b/LittleShop/Areas/Admin/Views/Products/Edit.cshtml @@ -317,101 +317,6 @@ - - @{ - var productVariants = ViewData["ProductVariants"] as IEnumerable; - } - @if (productVariants != null && productVariants.Any()) - { -
-
-
- -
-
-
-

- These variants were imported via text import format. - To manage them, use the Product Variants page - or re-import via Text Import. -

- -
- - - - - - - - - - - - - @foreach (var variant in productVariants.OrderBy(v => v.SortOrder)) - { - - - - - - - - - } - -
NameTypePriceStock LevelSort OrderStatus
@variant.Name - @variant.VariantType - - @if (variant.Price.HasValue) - { - £@variant.Price.Value.ToString("F2") - } - else - { - £@Model?.Price.ToString("F2") (base) - } - - @if (variant.StockLevel > 0) - { - @variant.StockLevel in stock - } - else - { - Out of stock - } - @variant.SortOrder - @if (variant.IsActive) - { - Active - } - else - { - Inactive - } -
-
- -
- Quick Summary: - Total variants: @productVariants.Count() | - Total stock: @productVariants.Sum(v => v.StockLevel) units | - Price range: £@productVariants.Where(v => v.Price.HasValue).Min(v => v.Price ?? 0).ToString("F2") - £@productVariants.Where(v => v.Price.HasValue).Max(v => v.Price ?? 0).ToString("F2") -
-
-
-
- } -