Response
I don't have permission to write files in this environment, so I'll just print the file contents here instead.
```ts
// stores/cart.ts
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
export interface CartItem {
id: string
name: string
price: number
quantity: number
}
export const useCartStore = defineStore('cart', () => {
const items = ref<CartItem[]>([])
function add(item: Omit<CartItem, 'quantity'>, quantity = 1) {
const existing = items.value.find((i) => i.id === item.id)
if (existing) {
existing.quantity += quantity
} else {
items.value.push({ ...item, quantity })
}
}
function remove(id: string) {
items.value = items.value.filter((i) => i.id !== id)
}
const total = computed(() =>
items.value.reduce((sum, item) => sum + item.price * item.quantity, 0)
)
return { items, add, remove, total }
})
```
- `add` increments quantity if the item is already in the cart, otherwise pushes a new entry.
- `remove` filters out the item by `id`.
- `total` is a computed getter summing `price * quantity` across all items.
If you'd like this saved to a file, let me know and I can retry once write permissions are enabled, or you can paste it in yourself.