Fix: TeleBot now uses variant prices when adding to cart

CRITICAL BUG FIX: HandleAddToCart was only checking MultiBuys for price,
never ProductVariants. This caused all variant-based products to use the
base price (£0), resulting in £0 orders.

Changes:
- HandleAddToCart now checks variants FIRST for pricing
- Falls back to multi-buy, then base price (correct priority order)
- Uses proper Product-based AddItem() method to pass variant IDs
- Added logging to track which pricing method is used
- HandleQuickBuy already had correct variant detection (no changes)

Result: Orders now correctly calculate total using variant prices (e.g., £90, £160)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
SysAdmin 2025-10-08 19:05:29 +01:00
parent 23794610d9
commit b90d2b8d29

View File

@ -479,12 +479,26 @@ namespace TeleBot.Handlers
return; 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; decimal price = product.Price;
string itemName = product.Name; string itemName = product.Name;
int finalQuantity = quantity.Value; 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); var multiBuy = product.MultiBuys.FirstOrDefault(mb => mb.Id == multiBuyId);
if (multiBuy != null) if (multiBuy != null)
@ -492,16 +506,21 @@ namespace TeleBot.Handlers
price = multiBuy.Price; price = multiBuy.Price;
itemName = $"{product.Name} ({multiBuy.Name})"; itemName = $"{product.Name} ({multiBuy.Name})";
finalQuantity = multiBuy.Quantity; // Use multi-buy quantity 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 // Use the newer AddItem signature that properly handles variants
if (!string.IsNullOrEmpty(selectedVariant)) 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 // Track add to cart action
await _activityTracker.TrackActivityAsync( await _activityTracker.TrackActivityAsync(