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") -
-
-
-
- } -