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(() => {
|
useEffect(() => {
|
||||||
const fetchDiscountCodes = async () => {
|
const fetchDiscountCodes = async () => {
|
||||||
try {
|
try {
|
||||||
|
console.log('Fetching discount codes from API...');
|
||||||
const response = await fetch('/api/discount-codes');
|
const response = await fetch('/api/discount-codes');
|
||||||
|
console.log('API response status:', response.status);
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const codes = await response.json();
|
const codes = await response.json();
|
||||||
|
console.log('Fetched discount codes:', codes);
|
||||||
setDiscountCodes(codes);
|
setDiscountCodes(codes);
|
||||||
|
} else {
|
||||||
|
console.error('Failed to fetch discount codes, status:', response.status);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch discount codes:', error);
|
console.error('Failed to fetch discount codes:', error);
|
||||||
@ -48,8 +54,21 @@ export default function CartView() {
|
|||||||
const total = subtotal - discount;
|
const total = subtotal - discount;
|
||||||
|
|
||||||
const applyDiscountCode = () => {
|
const applyDiscountCode = () => {
|
||||||
|
console.log('applyDiscountCode called');
|
||||||
|
console.log('discountCode:', discountCode);
|
||||||
|
console.log('available discountCodes:', discountCodes);
|
||||||
|
|
||||||
const code = discountCode.toLowerCase();
|
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) {
|
if (discountInfo) {
|
||||||
// Check if discount code has expired
|
// Check if discount code has expired
|
||||||
@ -57,6 +76,7 @@ export default function CartView() {
|
|||||||
const expirationDate = new Date(discountInfo.expiration);
|
const expirationDate = new Date(discountInfo.expiration);
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
if (now > expirationDate) {
|
if (now > expirationDate) {
|
||||||
|
console.log('discount code expired');
|
||||||
dispatch({
|
dispatch({
|
||||||
type: "SET_DISCOUNT",
|
type: "SET_DISCOUNT",
|
||||||
payload: { code: "", percentage: 0 },
|
payload: { code: "", percentage: 0 },
|
||||||
@ -65,11 +85,13 @@ export default function CartView() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('applying discount:', discountInfo.percentage);
|
||||||
dispatch({
|
dispatch({
|
||||||
type: "SET_DISCOUNT",
|
type: "SET_DISCOUNT",
|
||||||
payload: { code: discountCode, percentage: discountInfo.percentage },
|
payload: { code: discountCode, percentage: discountInfo.percentage },
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
console.log('discount code not found, clearing discount');
|
||||||
dispatch({ type: "SET_DISCOUNT", payload: { code: "", percentage: 0 } });
|
dispatch({ type: "SET_DISCOUNT", payload: { code: "", percentage: 0 } });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user