Feature: Add product variant price override support
Enables individual variants to have their own prices, overriding the base product price. **Database Changes:** - Added Price (decimal?, nullable) to ProductVariants table - Added ProductVariantId to OrderItems table with foreign key relationship - Created index on OrderItems.ProductVariantId for performance **API Changes:** - ProductVariantDto: Added Price field - CreateProductVariantDto: Added Price field with validation - UpdateProductVariantDto: Added Price field - OrderItemDto: Added ProductVariantId and ProductVariantName - CreateOrderItemDto: Added ProductVariantId **Business Logic:** - OrderService: Variant price overrides base price (but multi-buy takes precedence) - ProductService: All variant CRUD operations support Price field **Admin UI:** - CreateVariant: Price input with £ symbol and base price placeholder - EditVariant: Price editing with £ symbol - ProductVariants list: Shows variant price or "(base)" indicator **Client Library:** - Updated all DTOs to match server-side changes - Full support for variant pricing in order creation **Migration:** - EF Core migration: 20251003173458_AddVariantPricing - Backward compatible: NULL values supported for existing data **Use Case:** Products with size/color variants can now have different prices: - Small T-shirt: £15.00 (variant override) - Medium T-shirt: £18.00 (uses base price) - Large T-shirt: £20.00 (variant override) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -78,6 +78,8 @@ public class OrderService : IOrderService
|
||||
.ThenInclude(oi => oi.Product)
|
||||
.Include(o => o.Items)
|
||||
.ThenInclude(oi => oi.ProductMultiBuy)
|
||||
.Include(o => o.Items)
|
||||
.ThenInclude(oi => oi.ProductVariant)
|
||||
.Include(o => o.Payments)
|
||||
.FirstOrDefaultAsync(o => o.Id == id);
|
||||
|
||||
@@ -147,8 +149,25 @@ public class OrderService : IOrderService
|
||||
}
|
||||
|
||||
ProductMultiBuy? multiBuy = null;
|
||||
ProductVariant? variant = null;
|
||||
decimal unitPrice = product.Price;
|
||||
|
||||
// Check for variant price override (highest priority after multi-buy)
|
||||
if (itemDto.ProductVariantId.HasValue)
|
||||
{
|
||||
variant = await _context.ProductVariants.FindAsync(itemDto.ProductVariantId.Value);
|
||||
if (variant == null || !variant.IsActive || variant.ProductId != itemDto.ProductId)
|
||||
{
|
||||
throw new ArgumentException($"Product variant {itemDto.ProductVariantId} not found, inactive, or doesn't belong to product {itemDto.ProductId}");
|
||||
}
|
||||
|
||||
// Use variant price if it has an override, otherwise use product base price
|
||||
if (variant.Price.HasValue)
|
||||
{
|
||||
unitPrice = variant.Price.Value;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemDto.ProductMultiBuyId.HasValue)
|
||||
{
|
||||
multiBuy = await _context.ProductMultiBuys.FindAsync(itemDto.ProductMultiBuyId.Value);
|
||||
@@ -168,6 +187,7 @@ public class OrderService : IOrderService
|
||||
OrderId = order.Id,
|
||||
ProductId = itemDto.ProductId,
|
||||
ProductMultiBuyId = itemDto.ProductMultiBuyId,
|
||||
ProductVariantId = itemDto.ProductVariantId,
|
||||
Quantity = itemDto.Quantity,
|
||||
UnitPrice = unitPrice,
|
||||
TotalPrice = unitPrice * itemDto.Quantity
|
||||
@@ -331,8 +351,10 @@ public class OrderService : IOrderService
|
||||
Id = oi.Id,
|
||||
ProductId = oi.ProductId,
|
||||
ProductMultiBuyId = oi.ProductMultiBuyId,
|
||||
ProductVariantId = oi.ProductVariantId,
|
||||
ProductName = oi.Product.Name,
|
||||
ProductMultiBuyName = oi.ProductMultiBuy?.Name,
|
||||
ProductVariantName = oi.ProductVariant?.Name,
|
||||
Quantity = oi.Quantity,
|
||||
UnitPrice = oi.UnitPrice,
|
||||
TotalPrice = oi.TotalPrice
|
||||
|
||||
@@ -556,6 +556,7 @@ public class ProductService : IProductService
|
||||
VariantType = createVariantDto.VariantType,
|
||||
SortOrder = createVariantDto.SortOrder,
|
||||
StockLevel = createVariantDto.StockLevel,
|
||||
Price = createVariantDto.Price,
|
||||
IsActive = true,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
@@ -572,6 +573,7 @@ public class ProductService : IProductService
|
||||
VariantType = variant.VariantType,
|
||||
SortOrder = variant.SortOrder,
|
||||
StockLevel = variant.StockLevel,
|
||||
Price = variant.Price,
|
||||
IsActive = variant.IsActive,
|
||||
CreatedAt = variant.CreatedAt,
|
||||
UpdatedAt = variant.UpdatedAt
|
||||
@@ -595,6 +597,9 @@ public class ProductService : IProductService
|
||||
if (updateVariantDto.StockLevel.HasValue)
|
||||
variant.StockLevel = updateVariantDto.StockLevel.Value;
|
||||
|
||||
if (updateVariantDto.Price.HasValue)
|
||||
variant.Price = updateVariantDto.Price.Value;
|
||||
|
||||
if (updateVariantDto.IsActive.HasValue)
|
||||
variant.IsActive = updateVariantDto.IsActive.Value;
|
||||
|
||||
@@ -627,6 +632,7 @@ public class ProductService : IProductService
|
||||
VariantType = v.VariantType,
|
||||
SortOrder = v.SortOrder,
|
||||
StockLevel = v.StockLevel,
|
||||
Price = v.Price,
|
||||
IsActive = v.IsActive,
|
||||
CreatedAt = v.CreatedAt,
|
||||
UpdatedAt = v.UpdatedAt
|
||||
@@ -647,6 +653,7 @@ public class ProductService : IProductService
|
||||
VariantType = variant.VariantType,
|
||||
SortOrder = variant.SortOrder,
|
||||
StockLevel = variant.StockLevel,
|
||||
Price = variant.Price,
|
||||
IsActive = variant.IsActive,
|
||||
CreatedAt = variant.CreatedAt,
|
||||
UpdatedAt = variant.UpdatedAt
|
||||
|
||||
Reference in New Issue
Block a user