`;
return;
}
// Get user's selected region for currency
const selectedRegion = getSelectedRegion();
const targetCurrency = selectedRegion ? selectedRegion.currency : 'USD';
// Get exchange rate once for all products
const exchangeRate = await getExchangeRate('USD', targetCurrency);
// Fetch product details and calculate totals (all in cents)
// Use current prices from API, not stored prices
let subtotal = 0;
const itemsHtml = await Promise.all(items.map(async (item) => {
try {
const headers = {};
if (selectedRegion) {
headers['X-User-Region'] = selectedRegion.code;
}
const response = await fetch(`${API_BASE}/products/${item.productId}`, { headers });
const data = await response.json();
if (data.success) {
const product = data.product;
// Convert price to target currency
const currentPriceUSD = product.price; // In cents
const currentPrice = Math.round(currentPriceUSD * exchangeRate); // Converted to target currency
const itemTotal = currentPrice * item.quantity;
subtotal += itemTotal;
const image = product.image || '';
const imageElement = image && image.trim() && (image.startsWith('http') || image.startsWith('/'))
? ``
: '';
return `
${imageElement || '
No Image
'}
${escapeHtml(product.name)}
${targetCurrency} ${(currentPrice / 100).toFixed(2)} each
${targetCurrency} ${(itemTotal / 100).toFixed(2)}
`;
}
} catch (error) {
console.error('Error loading product:', error);
return '';
}
}));
// Tax cannot be calculated without country - show as uncalculated
const shipping = 0; // Can be calculated based on shipping info (in cents)
const tax = 0; // Tax cannot be calculated without country
const total = subtotal + shipping; // Tax will be calculated at checkout
content.innerHTML = `