TeleBot-variant-pricing

This commit is contained in:
sysadmin 2025-10-03 19:36:52 +01:00
parent 8385612bcd
commit e5f19f8b83
3 changed files with 45 additions and 10 deletions

View File

@ -620,9 +620,16 @@ namespace TeleBot.Handlers
// Add buttons for each variant with quickbuy flow
foreach (var variant in product.Variants.OrderBy(v => v.SortOrder))
{
var displayName = variant.Name;
// Show price if it's different from base product price
if (variant.Price.HasValue && variant.Price.Value != product.Price)
{
displayName += $" - £{variant.Price.Value:F2}";
}
buttons.Add(new[]
{
InlineKeyboardButton.WithCallbackData(variant.Name, $"quickbuyvar:{product.Id}:{quantity}:{variant.Name}")
InlineKeyboardButton.WithCallbackData(displayName, $"quickbuyvar:{product.Id}:{quantity}:{variant.Id}")
});
}
}
@ -643,10 +650,10 @@ namespace TeleBot.Handlers
private async Task HandleQuickBuyWithVariation(ITelegramBotClient bot, CallbackQuery callbackQuery, UserSession session, string[] data)
{
// Format: quickbuyvar:productId:quantity:variantName
// Format: quickbuyvar:productId:quantity:variantId
var productId = Guid.Parse(data[1]);
var quantity = int.Parse(data[2]);
var variantName = data[3];
var variantId = Guid.Parse(data[3]);
var product = await _shopService.GetProductAsync(productId);
if (product == null)
@ -655,9 +662,17 @@ namespace TeleBot.Handlers
return;
}
// Add to cart with variant
var itemName = $"{product.Name} - {variantName}";
session.Cart.AddItem(productId, itemName, product.Price, quantity, null, variantName);
// Find the variant
var variant = product.Variants?.FirstOrDefault(v => v.Id == variantId);
if (variant == null)
{
await bot.AnswerCallbackQueryAsync(callbackQuery.Id, "Variant not found", showAlert: true);
return;
}
// Add to cart with variant ID
var itemName = $"{product.Name} - {variant.Name}";
session.Cart.AddItem(product, quantity, multiBuyId: null, variantId: variantId);
await bot.AnswerCallbackQueryAsync(
callbackQuery.Id,
@ -1350,7 +1365,7 @@ namespace TeleBot.Handlers
if (product == null) return;
// Add to cart with selected variants
var cartItem = session.Cart.AddItem(product, quantity, multiBuyId, selectedVariants);
var cartItem = session.Cart.AddItem(product, quantity, multiBuyId, variantId: null, selectedVariants);
// Show success message
await bot.AnswerCallbackQueryAsync(

View File

@ -13,11 +13,27 @@ namespace TeleBot.Models
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
// New method that accepts Product and variants list
public CartItem AddItem(Product product, int quantity = 1, Guid? multiBuyId = null, List<string>? selectedVariants = null)
public CartItem AddItem(Product product, int quantity = 1, Guid? multiBuyId = null, Guid? variantId = null, List<string>? selectedVariants = null)
{
decimal price = product.Price;
Guid? productVariantId = null;
// If multi-buy selected, get the multi-buy price
// If variant selected, get the variant price override (if set)
if (variantId.HasValue && product.Variants != null)
{
var variant = product.Variants.FirstOrDefault(v => v.Id == variantId.Value);
if (variant != null)
{
productVariantId = variant.Id;
// Use variant price if it has an override, otherwise use product price
if (variant.Price.HasValue)
{
price = variant.Price.Value;
}
}
}
// If multi-buy selected, get the multi-buy price (overrides variant price)
if (multiBuyId.HasValue && product.MultiBuys != null)
{
var multiBuy = product.MultiBuys.FirstOrDefault(mb => mb.Id == multiBuyId.Value);
@ -37,6 +53,7 @@ namespace TeleBot.Models
var existingItem = Items.FirstOrDefault(i =>
i.ProductId == product.Id &&
i.MultiBuyId == multiBuyId &&
i.ProductVariantId == productVariantId &&
(i.SelectedVariant == variantKey ||
(i.SelectedVariants != null && string.Join(",", i.SelectedVariants.OrderBy(v => v)) == variantKey)));
@ -56,6 +73,7 @@ namespace TeleBot.Models
{
ProductId = product.Id,
MultiBuyId = multiBuyId,
ProductVariantId = productVariantId,
SelectedVariant = selectedVariants?.Count == 1 ? selectedVariants[0] : null,
SelectedVariants = selectedVariants ?? new List<string>(),
ProductName = product.Name,
@ -155,7 +173,8 @@ namespace TeleBot.Models
{
public Guid ProductId { get; set; }
public Guid? MultiBuyId { get; set; } // For quantity pricing (e.g., 3 for £25)
public string? SelectedVariant { get; set; } // For single items - one variant
public Guid? ProductVariantId { get; set; } // For variant with potential price override
public string? SelectedVariant { get; set; } // For single items - one variant (deprecated, use ProductVariantId)
public List<string> SelectedVariants { get; set; } = new(); // For multi-buys - multiple variants
public string ProductName { get; set; } = string.Empty;
public int Quantity { get; set; }

View File

@ -219,6 +219,7 @@ namespace TeleBot.Services
{
ProductId = i.ProductId,
ProductMultiBuyId = i.MultiBuyId,
ProductVariantId = i.ProductVariantId,
SelectedVariant = i.SelectedVariant,
Quantity = i.Quantity
}).ToList()