When coupon is applied, all the cart items should line through their old price and then under it show a new price with the coupon discount...
'use client';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import CartItem from './CartItem';
import { motion, AnimatePresence } from 'framer-motion';
import { useRouter } from "next/navigation";
import Loading from "@/components/Loading";
import { useAppDispatch, useAppSelector } from "@/lib/hook";
import { setOrder } from "@/lib/features/orderSlice";
import EmptyCart from "@/components/EmptyCart";
import { CircleArrowLeft } from "lucide-react";
To achieve the functionality where applying a coupon displays each cart item's original price with a line-through and the new discounted price below it, you'll need to make the following modifications to your existing Cart and CartItem components:
Calculate Discounted Prices per Item:
Depending on whether the discount is a percentage or a fixed amount, calculate the new price for each cart item.
Ensure that when a fixed discount is applied, it's distributed proportionally across all cart items based on their individual prices.
Pass Both Original and Discounted Prices to CartItem:
Update the props passed to each CartItem to include both the original price and the new discounted price.
Update CartItem to Display Both Prices:
Modify the CartItem component to conditionally render the original price with a line-through and show the discounted price below it when a discount is applied.
Below are the detailed steps and updated code for each component:
1. Modify the Cart Component
a. Calculate Per-Item Discounted Prices:
Add a useMemo hook to compute the discounted prices for each cart item based on the applied coupon. This ensures that the calculations are efficient and only recompute when dependencies change.
b. Update Props Passed to CartItem:
Update the CartItem component calls to include both originalPrice and discountedPrice.
c. Handle Different Discount Types:
Ensure that both percentage-based and fixed-amount discounts are correctly applied to each item.
A new useMemo hook named discountedCartItems calculates the originalPrice and discountedPrice for each cart item based on the applied coupon.
If the coupon provides a percentage discount, each item's price is reduced by that percentage.
If the coupon provides a fixed amount discount, the discount is distributed proportionally based on each item's total price.
Passing New Props to CartItem:
The CartItem component is now passed two additional props: originalPrice and discountedPrice.
These props are used to display both the original and discounted prices.
2. Update the CartItem Component
Modify the CartItem component to display both the original price (with a line-through) and the new discounted price when a discount is applied. If there's no discount, only the original price will be displayed.
originalPrice: The original price of the item before discount.
discountedPrice: The new price of the item after applying the discount.
Conditional Rendering:
If originalPrice differs from discountedPrice, both prices are displayed. The original price is shown with a line-through, and the discounted price is highlighted.
If there's no discount, only the original price is displayed normally.
Adjusted Price Calculation:
Both prices are multiplied by qty to reflect the total price for the quantity of items.
3. Additional Recommendations
Format Prices:
Consider formatting the prices to include commas for thousands separators or decimal points for better readability. You can create a utility function for price formatting.
javascript
const formatPrice = (price) => {
return new Intl.NumberFormat('en-US').format(price);
};
Handle Zero or Negative Prices:
Ensure that discounted prices do not go below zero, especially when dealing with fixed amount discounts.
Enhance User Feedback:
Provide visual cues or tooltips explaining the discount applied, especially for complex discount rules.
Optimize Performance:
If your cart becomes large, consider optimizing the discount calculation to prevent performance issues. Using useMemo as shown helps in this regard.
Final Thoughts
By implementing these changes, your cart will now display each item's original price with a line-through and the new discounted price beneath it when a coupon is applied. This enhances the user experience by clearly showing the benefits of applying a coupon and ensures transparency in pricing.
Feel free to further customize the styling to match your application's design and improve usability based on user feedback.