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:
SysAdmin 2025-10-03 18:35:43 +01:00
parent 68131b6549
commit d9efababa6
14 changed files with 1894 additions and 2 deletions

View File

@ -27,7 +27,9 @@ public class OrderItem
{
public Guid Id { get; set; }
public Guid ProductId { get; set; }
public Guid? ProductVariantId { get; set; }
public string? ProductName { get; set; }
public string? ProductVariantName { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal TotalPrice { get; set; }
@ -55,6 +57,7 @@ public class CreateOrderItem
{
public Guid ProductId { get; set; }
public Guid? ProductMultiBuyId { get; set; } // Optional: if specified, use multi-buy pricing
public string? SelectedVariant { get; set; } // Optional: variant choice (color/flavor)
public Guid? ProductVariantId { get; set; } // Optional: specific variant (used for variant price override)
public string? SelectedVariant { get; set; } // Optional: variant choice (color/flavor) - deprecated, use ProductVariantId
public int Quantity { get; set; }
}

View File

@ -49,5 +49,6 @@ public class ProductVariant
public int StockLevel { get; set; }
public decimal? Weight { get; set; } // Optional: override product weight
public int? WeightUnit { get; set; } // Optional: override product weight unit
public decimal? Price { get; set; } // Optional: override product price (if null, uses product.Price)
public bool IsActive { get; set; }
}

View File

@ -55,6 +55,16 @@
<small class="form-text text-muted">Track inventory for this specific variant (optional)</small>
</div>
<div class="mb-3">
<label asp-for="Price" class="form-label"></label>
<div class="input-group">
<span class="input-group-text">£</span>
<input asp-for="Price" type="number" step="0.01" class="form-control" min="0.01" placeholder="@product?.Price.ToString("F2")" />
</div>
<span asp-validation-for="Price" class="text-danger"></span>
<small class="form-text text-muted">Override base price for this variant (optional - defaults to £@product?.Price.ToString("F2"))</small>
</div>
<div class="mb-3">
<label asp-for="SortOrder" class="form-label"></label>
<input asp-for="SortOrder" type="number" class="form-control" />

View File

@ -51,6 +51,16 @@
<span asp-validation-for="StockLevel" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="Price" class="form-label"></label>
<div class="input-group">
<span class="input-group-text">£</span>
<input asp-for="Price" type="number" step="0.01" class="form-control" min="0.01" placeholder="@product?.Price.ToString("F2")" />
</div>
<span asp-validation-for="Price" class="text-danger"></span>
<small class="form-text text-muted">Override base price (optional - defaults to £@product?.Price.ToString("F2"))</small>
</div>
<div class="mb-3">
<label asp-for="SortOrder" class="form-label"></label>
<input asp-for="SortOrder" type="number" class="form-control" />

View File

@ -33,6 +33,7 @@
<tr>
<th>Name</th>
<th>Type</th>
<th>Price</th>
<th>Stock Level</th>
<th>Sort Order</th>
<th>Status</th>
@ -49,6 +50,16 @@
<td>
<span class="badge bg-info">@variant.VariantType</span>
</td>
<td>
@if (variant.Price.HasValue)
{
<strong>£@variant.Price.Value.ToString("F2")</strong>
}
else
{
<span class="text-muted">£@product?.Price.ToString("F2") (base)</span>
}
</td>
<td>
@if (variant.StockLevel > 0)
{

View File

@ -51,8 +51,10 @@ public class OrderItemDto
public Guid Id { get; set; }
public Guid ProductId { get; set; }
public Guid? ProductMultiBuyId { get; set; }
public Guid? ProductVariantId { get; set; }
public string ProductName { get; set; } = string.Empty;
public string? ProductMultiBuyName { get; set; }
public string? ProductVariantName { get; set; }
public string? SelectedVariant { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
@ -97,7 +99,9 @@ public class CreateOrderItemDto
public Guid? ProductMultiBuyId { get; set; } // Optional: if specified, use multi-buy pricing
public string? SelectedVariant { get; set; } // Optional: variant choice (color/flavor)
public Guid? ProductVariantId { get; set; } // Optional: specific variant (used for variant price override)
public string? SelectedVariant { get; set; } // Optional: variant choice (color/flavor) - deprecated, use ProductVariantId
[Range(1, int.MaxValue)]
public int Quantity { get; set; }

View File

@ -125,6 +125,7 @@ public class ProductVariantDto
public string VariantType { get; set; } = "Standard";
public int SortOrder { get; set; }
public int StockLevel { get; set; }
public decimal? Price { get; set; } // If null, uses product.Price
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
@ -170,6 +171,9 @@ public class CreateProductVariantDto
[Range(0, int.MaxValue)]
public int StockLevel { get; set; } = 0;
[Range(0.01, double.MaxValue, ErrorMessage = "Price must be greater than 0")]
public decimal? Price { get; set; } // Optional: If null, uses product.Price
}
public class UpdateProductMultiBuyDto
@ -205,5 +209,8 @@ public class UpdateProductVariantDto
[Range(0, int.MaxValue)]
public int? StockLevel { get; set; }
[Range(0.01, double.MaxValue, ErrorMessage = "Price must be greater than 0")]
public decimal? Price { get; set; } // Optional: If null, uses product.Price
public bool? IsActive { get; set; }
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,59 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace LittleShop.Migrations
{
/// <inheritdoc />
public partial class AddVariantPricing : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<decimal>(
name: "Price",
table: "ProductVariants",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "ProductVariantId",
table: "OrderItems",
type: "TEXT",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_OrderItems_ProductVariantId",
table: "OrderItems",
column: "ProductVariantId");
migrationBuilder.AddForeignKey(
name: "FK_OrderItems_ProductVariants_ProductVariantId",
table: "OrderItems",
column: "ProductVariantId",
principalTable: "ProductVariants",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_OrderItems_ProductVariants_ProductVariantId",
table: "OrderItems");
migrationBuilder.DropIndex(
name: "IX_OrderItems_ProductVariantId",
table: "OrderItems");
migrationBuilder.DropColumn(
name: "Price",
table: "ProductVariants");
migrationBuilder.DropColumn(
name: "ProductVariantId",
table: "OrderItems");
}
}
}

View File

@ -859,6 +859,9 @@ namespace LittleShop.Migrations
b.Property<Guid?>("ProductMultiBuyId")
.HasColumnType("TEXT");
b.Property<Guid?>("ProductVariantId")
.HasColumnType("TEXT");
b.Property<int>("Quantity")
.HasColumnType("INTEGER");
@ -880,6 +883,8 @@ namespace LittleShop.Migrations
b.HasIndex("ProductMultiBuyId");
b.HasIndex("ProductVariantId");
b.ToTable("OrderItems");
});
@ -1041,6 +1046,9 @@ namespace LittleShop.Migrations
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<decimal?>("Price")
.HasColumnType("TEXT");
b.Property<Guid>("ProductId")
.HasColumnType("TEXT");
@ -1537,11 +1545,17 @@ namespace LittleShop.Migrations
.HasForeignKey("ProductMultiBuyId")
.OnDelete(DeleteBehavior.Restrict);
b.HasOne("LittleShop.Models.ProductVariant", "ProductVariant")
.WithMany()
.HasForeignKey("ProductVariantId");
b.Navigation("Order");
b.Navigation("Product");
b.Navigation("ProductMultiBuy");
b.Navigation("ProductVariant");
});
modelBuilder.Entity("LittleShop.Models.Product", b =>

View File

@ -14,6 +14,8 @@ public class OrderItem
public Guid? ProductMultiBuyId { get; set; } // Nullable for backward compatibility
public Guid? ProductVariantId { get; set; } // Optional: specific variant with potential price override
[StringLength(500)]
public string? SelectedVariants { get; set; } // JSON array of variants chosen (e.g., ["Red", "Blue", "Green"] for multi-buy)
@ -29,4 +31,5 @@ public class OrderItem
public virtual Order Order { get; set; } = null!;
public virtual Product Product { get; set; } = null!;
public virtual ProductMultiBuy? ProductMultiBuy { get; set; }
public virtual ProductVariant? ProductVariant { get; set; }
}

View File

@ -27,6 +27,8 @@ public class ProductVariant
public ProductWeightUnit? WeightUnit { get; set; } // Optional: override product weight unit for this variant
public decimal? Price { get; set; } // Optional: override product price for this variant (if null, uses product.Price)
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;

View File

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

View File

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