fix discount codes not applying due to case sensitivity
This commit is contained in:
parent
ca371e1006
commit
5968c82ae0
@ -13,10 +13,16 @@ export default function CartView() {
|
||||
useEffect(() => {
|
||||
const fetchDiscountCodes = async () => {
|
||||
try {
|
||||
console.log('Fetching discount codes from API...');
|
||||
const response = await fetch('/api/discount-codes');
|
||||
console.log('API response status:', response.status);
|
||||
|
||||
if (response.ok) {
|
||||
const codes = await response.json();
|
||||
console.log('Fetched discount codes:', codes);
|
||||
setDiscountCodes(codes);
|
||||
} else {
|
||||
console.error('Failed to fetch discount codes, status:', response.status);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch discount codes:', error);
|
||||
@ -48,8 +54,21 @@ export default function CartView() {
|
||||
const total = subtotal - discount;
|
||||
|
||||
const applyDiscountCode = () => {
|
||||
console.log('applyDiscountCode called');
|
||||
console.log('discountCode:', discountCode);
|
||||
console.log('available discountCodes:', discountCodes);
|
||||
|
||||
const code = discountCode.toLowerCase();
|
||||
const discountInfo = (discountCodes as Record<string, DiscountCode>)[code];
|
||||
// Make case-insensitive lookup by checking all keys
|
||||
const discountCodesLowerCase = Object.keys(discountCodes).reduce((acc, key) => {
|
||||
acc[key.toLowerCase()] = discountCodes[key];
|
||||
return acc;
|
||||
}, {} as Record<string, DiscountCode>);
|
||||
|
||||
const discountInfo = discountCodesLowerCase[code];
|
||||
|
||||
console.log('looking for code:', code);
|
||||
console.log('found discountInfo:', discountInfo);
|
||||
|
||||
if (discountInfo) {
|
||||
// Check if discount code has expired
|
||||
@ -57,6 +76,7 @@ export default function CartView() {
|
||||
const expirationDate = new Date(discountInfo.expiration);
|
||||
const now = new Date();
|
||||
if (now > expirationDate) {
|
||||
console.log('discount code expired');
|
||||
dispatch({
|
||||
type: "SET_DISCOUNT",
|
||||
payload: { code: "", percentage: 0 },
|
||||
@ -65,11 +85,13 @@ export default function CartView() {
|
||||
}
|
||||
}
|
||||
|
||||
console.log('applying discount:', discountInfo.percentage);
|
||||
dispatch({
|
||||
type: "SET_DISCOUNT",
|
||||
payload: { code: discountCode, percentage: discountInfo.percentage },
|
||||
});
|
||||
} else {
|
||||
console.log('discount code not found, clearing discount');
|
||||
dispatch({ type: "SET_DISCOUNT", payload: { code: "", percentage: 0 } });
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user