From b90d2b8d290cf0a1d2f8ec2fe6a0e9ac70e463ec Mon Sep 17 00:00:00 2001 From: SysAdmin Date: Wed, 8 Oct 2025 19:05:29 +0100 Subject: [PATCH] Fix: TeleBot now uses variant prices when adding to cart MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- TeleBot/TeleBot/Handlers/CallbackHandler.cs | 33 ++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) 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(