feat: Add quantity controls to cart with +/- buttons

This commit is contained in:
2026-03-07 17:57:31 -05:00
parent 339ac6727f
commit 2ba8004240
2 changed files with 71 additions and 2 deletions

View File

@@ -21,8 +21,26 @@
</template> </template>
<v-list-item-title>{{ item.name }}</v-list-item-title> <v-list-item-title>{{ item.name }}</v-list-item-title>
<v-list-item-subtitle> <v-list-item-subtitle class="d-flex align-center">
{{ item.quantity }} x {{ currency(item.price) }} <div class="quantity-controls">
<v-btn small text class="qty-btn" @click="decreaseQuantity(item.id)"
><v-icon small>mdi-minus</v-icon></v-btn
>
<v-text-field
:model-value="item.quantity"
@update:model-value="updateQuantity(item.id, $event)"
type="number"
min="1"
density="compact"
variant="outlined"
hide-details
class="qty-input"
/>
<v-btn small text class="qty-btn" @click="increaseQuantity(item.id)"
><v-icon small>mdi-plus</v-icon></v-btn
>
</div>
<span class="ml-2">x {{ currency(item.price) }}</span>
</v-list-item-subtitle> </v-list-item-subtitle>
<template v-slot:append> <template v-slot:append>
@@ -71,6 +89,7 @@ export default {
required: true required: true
} }
}, },
emits: ['remove', 'checkout', 'update-quantity'],
computed: { computed: {
cartCount() { cartCount() {
return this.cartItems.reduce((sum, item) => sum + item.quantity, 0); return this.cartItems.reduce((sum, item) => sum + item.quantity, 0);
@@ -78,6 +97,45 @@ export default {
cartTotal() { cartTotal() {
return this.cartItems.reduce((sum, item) => sum + (item.price * item.quantity), 0); return this.cartItems.reduce((sum, item) => sum + (item.price * item.quantity), 0);
} }
},
methods: {
updateQuantity(itemId, newQuantity) {
const qty = parseInt(newQuantity) || 1;
this.$emit('update-quantity', { itemId, quantity: qty });
},
increaseQuantity(itemId) {
const item = this.cartItems.find(i => i.id === itemId);
if (item) {
this.$emit('update-quantity', { itemId, quantity: item.quantity + 1 });
}
},
decreaseQuantity(itemId) {
const item = this.cartItems.find(i => i.id === itemId);
if (item && item.quantity > 1) {
this.$emit('update-quantity', { itemId, quantity: item.quantity - 1 });
} else if (item) {
this.$emit('remove', itemId);
}
}
} }
} }
</script> </script>
<style scoped>
.qty-input {
width: 70px;
}
.qty-input input {
text-align: center;
}
.quantity-controls {
display: inline-flex;
align-items: center;
gap: 4px;
}
.qty-btn {
min-width: 28px !important;
height: 28px !important;
border-radius: 14px !important;
}
</style>

View File

@@ -17,6 +17,7 @@
:currency="currency" :currency="currency"
@remove="removeFromCart" @remove="removeFromCart"
@checkout="goToCheckout" @checkout="goToCheckout"
@update-quantity="updateCartQuantity"
/> />
</div> </div>
</v-col> </v-col>
@@ -98,6 +99,16 @@ export default {
item.quantity = 0; item.quantity = 0;
} }
}, },
updateCartQuantity({ itemId, quantity }) {
const cartItem = this.cartItems.find(i => i.id === itemId);
if (cartItem) {
cartItem.quantity = quantity;
}
const productItem = this.items.find(i => i.id === itemId);
if (productItem) {
productItem.quantity = quantity;
}
},
goToCheckout() { goToCheckout() {
this.$router.push('/comprar'); this.$router.push('/comprar');
}, },