Fix: Populate existing variant panels with imported ProductVariant data

**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 <noreply@anthropic.com>
This commit is contained in:
2025-10-08 18:05:18 +01:00
parent 2c5815510d
commit 9f7b2840af
2 changed files with 14 additions and 97 deletions

View File

@@ -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
};