English 中文(简体)
Discount doesn t decrease the total price, it stays the same even though the applied discount code is valid, what am I doing wrong?
原标题:

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);
        }
    }

}

}



问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签