TeleBot-variant-pricing
This commit is contained in:
parent
8385612bcd
commit
e5f19f8b83
@ -620,9 +620,16 @@ namespace TeleBot.Handlers
|
|||||||
// Add buttons for each variant with quickbuy flow
|
// Add buttons for each variant with quickbuy flow
|
||||||
foreach (var variant in product.Variants.OrderBy(v => v.SortOrder))
|
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[]
|
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)
|
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 productId = Guid.Parse(data[1]);
|
||||||
var quantity = int.Parse(data[2]);
|
var quantity = int.Parse(data[2]);
|
||||||
var variantName = data[3];
|
var variantId = Guid.Parse(data[3]);
|
||||||
|
|
||||||
var product = await _shopService.GetProductAsync(productId);
|
var product = await _shopService.GetProductAsync(productId);
|
||||||
if (product == null)
|
if (product == null)
|
||||||
@ -655,9 +662,17 @@ namespace TeleBot.Handlers
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add to cart with variant
|
// Find the variant
|
||||||
var itemName = $"{product.Name} - {variantName}";
|
var variant = product.Variants?.FirstOrDefault(v => v.Id == variantId);
|
||||||
session.Cart.AddItem(productId, itemName, product.Price, quantity, null, variantName);
|
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(
|
await bot.AnswerCallbackQueryAsync(
|
||||||
callbackQuery.Id,
|
callbackQuery.Id,
|
||||||
@ -1350,7 +1365,7 @@ namespace TeleBot.Handlers
|
|||||||
if (product == null) return;
|
if (product == null) return;
|
||||||
|
|
||||||
// Add to cart with selected variants
|
// 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
|
// Show success message
|
||||||
await bot.AnswerCallbackQueryAsync(
|
await bot.AnswerCallbackQueryAsync(
|
||||||
|
|||||||
@ -13,11 +13,27 @@ namespace TeleBot.Models
|
|||||||
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
|
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
|
||||||
|
|
||||||
// New method that accepts Product and variants list
|
// 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;
|
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)
|
if (multiBuyId.HasValue && product.MultiBuys != null)
|
||||||
{
|
{
|
||||||
var multiBuy = product.MultiBuys.FirstOrDefault(mb => mb.Id == multiBuyId.Value);
|
var multiBuy = product.MultiBuys.FirstOrDefault(mb => mb.Id == multiBuyId.Value);
|
||||||
@ -37,6 +53,7 @@ namespace TeleBot.Models
|
|||||||
var existingItem = Items.FirstOrDefault(i =>
|
var existingItem = Items.FirstOrDefault(i =>
|
||||||
i.ProductId == product.Id &&
|
i.ProductId == product.Id &&
|
||||||
i.MultiBuyId == multiBuyId &&
|
i.MultiBuyId == multiBuyId &&
|
||||||
|
i.ProductVariantId == productVariantId &&
|
||||||
(i.SelectedVariant == variantKey ||
|
(i.SelectedVariant == variantKey ||
|
||||||
(i.SelectedVariants != null && string.Join(",", i.SelectedVariants.OrderBy(v => v)) == variantKey)));
|
(i.SelectedVariants != null && string.Join(",", i.SelectedVariants.OrderBy(v => v)) == variantKey)));
|
||||||
|
|
||||||
@ -56,6 +73,7 @@ namespace TeleBot.Models
|
|||||||
{
|
{
|
||||||
ProductId = product.Id,
|
ProductId = product.Id,
|
||||||
MultiBuyId = multiBuyId,
|
MultiBuyId = multiBuyId,
|
||||||
|
ProductVariantId = productVariantId,
|
||||||
SelectedVariant = selectedVariants?.Count == 1 ? selectedVariants[0] : null,
|
SelectedVariant = selectedVariants?.Count == 1 ? selectedVariants[0] : null,
|
||||||
SelectedVariants = selectedVariants ?? new List<string>(),
|
SelectedVariants = selectedVariants ?? new List<string>(),
|
||||||
ProductName = product.Name,
|
ProductName = product.Name,
|
||||||
@ -155,7 +173,8 @@ namespace TeleBot.Models
|
|||||||
{
|
{
|
||||||
public Guid ProductId { get; set; }
|
public Guid ProductId { get; set; }
|
||||||
public Guid? MultiBuyId { get; set; } // For quantity pricing (e.g., 3 for £25)
|
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 List<string> SelectedVariants { get; set; } = new(); // For multi-buys - multiple variants
|
||||||
public string ProductName { get; set; } = string.Empty;
|
public string ProductName { get; set; } = string.Empty;
|
||||||
public int Quantity { get; set; }
|
public int Quantity { get; set; }
|
||||||
|
|||||||
@ -219,6 +219,7 @@ namespace TeleBot.Services
|
|||||||
{
|
{
|
||||||
ProductId = i.ProductId,
|
ProductId = i.ProductId,
|
||||||
ProductMultiBuyId = i.MultiBuyId,
|
ProductMultiBuyId = i.MultiBuyId,
|
||||||
|
ProductVariantId = i.ProductVariantId,
|
||||||
SelectedVariant = i.SelectedVariant,
|
SelectedVariant = i.SelectedVariant,
|
||||||
Quantity = i.Quantity
|
Quantity = i.Quantity
|
||||||
}).ToList()
|
}).ToList()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user