# Cart Pricing Fix - Instructions

## Issue
Cart items were being added with `one_time_fee` field, but checkout expects `installation_fee`, `router_purchase_price`, and `extension_cost` separately.

## What Was Fixed
Updated `afinet-portal/src/app/(portal)/products/page.js` line 206-213 to use correct field names.

## How to Test the Fix

### Option 1: Clear Cart and Re-add (Recommended)
1. Open browser console (F12)
2. Run this command to clear cart:
   ```javascript
   localStorage.removeItem('cart-storage')
   ```
3. Refresh the page
4. Go to Products page
5. Add Starlink (or any package) to cart
6. Go to Checkout
7. **Result**: Should now show correct installation fee and total

### Option 2: Manual Cart Migration
If you want to keep existing cart items, run this in browser console:

```javascript
// Get current cart
const cartData = JSON.parse(localStorage.getItem('cart-storage'));

// Migrate items
if (cartData && cartData.state && cartData.state.items) {
  cartData.state.items = cartData.state.items.map(item => {
    // If item has old one_time_fee field, split it
    if (item.one_time_fee !== undefined) {
      return {
        ...item,
        installation_fee: item.one_time_fee || 0,
        router_purchase_price: 0,
        extension_cost: 0,
        routerOption: 'purchase'
      };
    }
    // If item already has correct fields, keep as is
    return item;
  });
  
  // Save migrated cart
  localStorage.setItem('cart-storage', JSON.stringify(cartData));
  
  console.log('Cart migrated successfully!');
  location.reload();
}
```

## Expected Result After Fix

### Before (Wrong):
```
Recurring Monthly: $120.00
Initial One-Time Charges: $0.00
VAT (16%): $0.00
Total due today: $0.00
```

### After (Correct):
```
Recurring Monthly: $120.00
Initial One-Time Charges: $150.00  ← Now shows correct amount
VAT (16%): $24.00                  ← Calculated on initial charges
Total due today: $174.00           ← Correct total
```

## What Changed in Code

### Before:
```javascript
addToCart({
  id: product.id,
  name: product.name,
  monthly_price: parseFloat(product.monthly_price),
  one_time_fee: parseFloat(product.installation_fee || 0) + parseFloat(product.router_purchase_price || 0),
  quantity: 1,
});
```

### After:
```javascript
addToCart({
  id: product.id,
  name: product.name,
  monthly_price: parseFloat(product.monthly_price),
  installation_fee: parseFloat(product.installation_fee || 0),
  router_purchase_price: parseFloat(product.router_purchase_price || 0),
  extension_cost: 0,
  routerOption: 'purchase',
  quantity: 1,
});
```

## Files Modified
- ✅ `afinet-portal/src/app/(portal)/products/page.js` (line 206-213) - Fixed addToCart to use correct fields
- ✅ `afinet-portal/src/app/(portal)/cart/page.js` (line 268-285) - Fixed item display to show correct one-time fee

## Files Already Correct
- ✅ `afinet-portal/src/app/(portal)/products/[id]/page.js` (already had correct fields)
- ✅ `afinet-portal/src/app/(portal)/checkout/page.js` (expects correct fields)

## Verification Steps

1. **Clear cart**: `localStorage.removeItem('cart-storage')`
2. **Add Starlink to cart** from products page
3. **Check cart data** in console:
   ```javascript
   JSON.parse(localStorage.getItem('cart-storage'))
   ```
4. **Verify fields exist**:
   - `installation_fee` ✓
   - `router_purchase_price` ✓
   - `extension_cost` ✓
5. **Go to checkout** and verify totals display correctly

## Quick Test Command

Run this in browser console to check if cart has correct structure:

```javascript
const cart = JSON.parse(localStorage.getItem('cart-storage'));
const item = cart?.state?.items?.[0];

if (item) {
  console.log('Cart item structure:', {
    hasInstallationFee: 'installation_fee' in item,
    hasRouterPrice: 'router_purchase_price' in item,
    hasExtensionCost: 'extension_cost' in item,
    hasOldOneTimeFee: 'one_time_fee' in item,
    values: {
      installation_fee: item.installation_fee,
      router_purchase_price: item.router_purchase_price,
      extension_cost: item.extension_cost
    }
  });
  
  if ('one_time_fee' in item) {
    console.warn('⚠️ Cart has OLD structure - clear cart and re-add items');
  } else {
    console.log('✅ Cart has CORRECT structure');
  }
} else {
  console.log('Cart is empty');
}
```

## Status
✅ **COMPLETE** - All code updated to use correct field names
✅ **Cart Display Fixed** - Now shows correct one-time fees calculated from installation_fee + router_purchase_price + extension_cost
⚠️ **Action Required** - Clear existing cart and re-add items to see the fix

---

**Next Step**: Clear your cart and re-add Starlink to see the correct pricing!
