diff --git a/TeleBot/TeleBot/Handlers/CallbackHandler.cs b/TeleBot/TeleBot/Handlers/CallbackHandler.cs index 96c21f6..7707aaf 100644 --- a/TeleBot/TeleBot/Handlers/CallbackHandler.cs +++ b/TeleBot/TeleBot/Handlers/CallbackHandler.cs @@ -479,12 +479,26 @@ namespace TeleBot.Handlers return; } - // Get price based on multi-buy or base product + // Get price based on variant, multi-buy, or base product (priority order) decimal price = product.Price; string itemName = product.Name; int finalQuantity = quantity.Value; + Guid? variantId = null; - if (multiBuyId.HasValue && product.MultiBuys != null) + // Priority 1: Variant price (if variant selected by name - legacy string-based variants) + if (!string.IsNullOrEmpty(selectedVariant) && product.Variants?.Any() == true) + { + var variant = product.Variants.FirstOrDefault(v => v.Name == selectedVariant); + if (variant != null) + { + price = variant.Price ?? product.Price; // Use variant price or fallback to product price + variantId = variant.Id; + itemName = $"{product.Name} - {variant.Name}"; + _logger.LogInformation("Using variant price: {Variant} at £{Price}", variant.Name, price); + } + } + // Priority 2: Multi-buy price + else if (multiBuyId.HasValue && product.MultiBuys != null) { var multiBuy = product.MultiBuys.FirstOrDefault(mb => mb.Id == multiBuyId); if (multiBuy != null) @@ -492,16 +506,21 @@ namespace TeleBot.Handlers price = multiBuy.Price; itemName = $"{product.Name} ({multiBuy.Name})"; finalQuantity = multiBuy.Quantity; // Use multi-buy quantity + _logger.LogInformation("Using multi-buy price: {MultiBuy} at £{Price}", multiBuy.Name, price); } } - // Add variant to item name if selected - if (!string.IsNullOrEmpty(selectedVariant)) + // Use the newer AddItem signature that properly handles variants + if (variantId.HasValue) { - itemName += $" - {selectedVariant}"; + // Use the Product-based AddItem method for proper variant tracking + session.Cart.AddItem(product, finalQuantity, multiBuyId, variantId); + } + else + { + // Legacy method for backward compatibility (no variant) + session.Cart.AddItem(productId.Value, itemName, price, finalQuantity, multiBuyId, selectedVariant); } - - session.Cart.AddItem(productId.Value, itemName, price, finalQuantity, multiBuyId, selectedVariant); // Track add to cart action await _activityTracker.TrackActivityAsync(