I want to apply the ability for the user to get a discount on their total price based on the discountCode they applied, but it doesn t do anything for it. Help me out please, the more I m touching it, the worse it seems to get.
I did debugging, I think it goes wrong within the update cart total price method, it has all the right information of the discount, the percentage etc. but it doesn t do anything with it. It just returns the same total price as before:
the cart page model with the apply discount code method:
[HttpPost]
public IActionResult OnPostApplyDiscount()
{
var customerID = GetCustomerIDFromTempData();
if (customerID == null)
{
return RedirectToLoginPage();
}
var discountCode = _discountCodeManager.GetDiscountByCode(DiscountedCode);
if (discountCode == null)
{
TempData["ErrorMessage"] = "Invalid discount code.";
return RedirectToCartPage(customerID.Value);
}
if (!_discountCodeManager.ValidateDiscountCode(discountCode.Code))
{
TempData["ErrorMessage"] = "Discount code is not valid.";
return RedirectToCartPage(customerID.Value);
}
_cartManager.ApplyDiscountCode(customerID.Value, discountCode.Code);
_cartManager.UpdateCartTotalPrice(customerID.Value, TotalPrice);
TempData["SuccessMessage"] = "Discount code applied successfully.";
Cart = _cartManager.GetCart(customerID.Value);
ViewData["DiscountApplied"] = "true";
ViewData["DiscountedTotalPrice"] = _cartManager.CalculateTotalPrice(Cart);
return RedirectToCartPage(customerID.Value);
}
<tfoot>
<tr>
<th></th>
<th></th>
<th></th>
<th>Total:</th>
<th class="@(ViewData["DiscountApplied"] != null ? "discounted-price" : "")">
€@(ViewData["DiscountApplied"] != null ? ViewData["DiscountedTotalPrice"] : Model.TotalPrice).ToString("N2")
</th>
</tr>
</tfoot>
The cartManager and discountCodeManager with the logic and calculations:
public decimal ApplyDiscountToCart(decimal totalPrice, decimal discountPercentage)
{
decimal discountAmount = totalPrice * (discountPercentage / 100);
decimal discountedPrice = totalPrice - discountAmount;
return discountedPrice;
}
public DiscountCode GetDiscountByCode(string code)
{
return _discountCodeRepository.GetDiscountByCode(code);
}
public decimal ApplyBuyOneGetOneFreeDiscount(int quantity, decimal itemPrice)
{
int itemsToPayFor = quantity / 2;
decimal discountedPrice = itemsToPayFor * itemPrice;
return discountedPrice;
}
public decimal ApplyDiscounts(decimal price)
{
decimal discount = 0;
// Discount 1: 10% off for prices above $100
if (price > 100)
{
discount += price * 0.1m;
}
// Discount 2: 20% off for prices between $200 and $300
if (price >= 200 && price <= 300)
{
discount += price * 0.2m;
}
// Discount 3: $50 off for prices over $500
if (price > 500)
{
discount += 50;
}
// Apply the accumulated discounts
price -= discount;
return price;
}
public bool ValidateDiscountCode(string code)
{
return _discountCodeRepository.ValidateDiscountCode(code);
}
public void CheckForExpiredDiscounts(Cart cart)
{
foreach (var item in cart.Items)
{
if (_discountCodeRepository.HasDiscountCodeExpired(item.DiscountCodeID))
{
item.DiscountedPercentage = 0;
item.Price = _cartManager.GetPerfumePrice(item.PerfumeID);
}
}
}
}
}
public decimal CalculateTotalPrice(Cart cart) { decimal totalPrice = 0;
foreach (var item in cart.Items)
{
decimal itemPrice = _cartRepository.GetPerfumePrice(item.PerfumeID);
if (item.DiscountedPercentage > 0)
{
itemPrice = itemPrice * (1 - item.DiscountedPercentage / 100m);
}
totalPrice += item.Quantity * itemPrice;
}
return totalPrice;
}
public decimal UpdateCartTotalPrice(int customerID, decimal totalPrice)
{
var cartItems = _cartRepository.GetCartItems(customerID);
decimal totalDiscountedPrice = 0;
foreach (var item in cartItems)
{
decimal itemPrice = _cartRepository.GetPerfumePrice(item.PerfumeID);
if (item.DiscountedPercentage > 0)
{
itemPrice = itemPrice * (1 - item.DiscountedPercentage / 100m);
}
else
{
itemPrice = itemPrice * 0.9m;
}
item.Price = itemPrice;
totalDiscountedPrice += item.Quantity * itemPrice;
}
_cartRepository.UpdateCartTotalPrice(customerID, totalDiscountedPrice);
return totalDiscountedPrice;
}
public void UpdateCartItemWithDiscount(CartItem cartItem)
{
_cartRepository.UpdateCartItemWithDiscount(cartItem);
var cart = GetCartByCustomerId(cartItem.CustomerID);
var totalPrice = CalculateTotalPrice(cart);
UpdateCartTotalPrice(cart.CustomerID, totalPrice);
}
public void ApplyDiscountCode(int customerID, string discountCode)
{
var cartItems = _cartRepository.GetCartItemsByCustomerId(customerID);
foreach (var item in cartItems)
{
item.DiscountedPercentage = 0;
item.DiscountCodeID = 0;
}
var discount = _discountCodeManager.GetDiscountByCode(discountCode);
if (discount != null)
{
foreach (var item in cartItems)
{
item.DiscountedPercentage = discount.Percentage;
item.DiscountCodeID = discount.DiscountCodeId;
_cartRepository.UpdateCartItemWithDiscount(item);
}
var totalPrice = CalculateTotalPrice(GetCartByCustomerId(customerID));
UpdateCartTotalPrice(customerID, totalPrice);
}
}
}
}