Search⌘ K
AI Features

Add to Wishlist Button

Learn to implement an add to wishlist button on the product detail page in a Vue.js frontend. This lesson guides you through fetching tokens, calling the wishlist API with a product ID, and providing user feedback with alerts to enhance the shopping experience.

In this lesson, we’ll learn to build a button to add items into the wishlist.

First, we’ll create a button on the product detail page and call the add to wishlist API.

Adding the button

We’ll add a simple button, which will trigger the addToWishList function when we click it. We define it below:

C++
<template>
<div class="container">
<div class="row pt-5">
<!-- Leave some empty space in left-->
<div class="col-md-1">
</div>
<!-- Display the image in left side-->
<div class="col-md-4 col-12">
<img :src="product.imageURL" :alt="product.name" class="img-fluid">
</div>
<!-- Display product name category name and product description-->
<div class="col-md-6 col-12 pt-3 pt-md-0">
<h4>{{product.name}}</h4>
<h6 class="category font-italic">{{category.categoryName}}</h6>
<p><span class="font-weight-bold">Description: -</span> <br>{{product.description}}</p>
<div class="d-flex flex-row justify-content-between">
<div class="input-group col-md-3 col-4 p-0">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">Quantity</span>
</div>
<input class="form-control" type="number" v-model="quantity"/>
</div>
<!-- wishlist button -->
<button id="wishlist-button" class="btn mr-3 p-1 py-0" style="background-color: #b3a594"
@click="addToWishList(this.id)">
Add to wishlist
</button>
</div>
<!-- Dummy placeholder features -->
<div class="features pt-3">
<h5><strong>Features</strong></h5>
<ul>
<li>Lorem ipsum dolor sit amet consectetur adipisicing elit.</li>
<li>Officia quas, officiis eius magni error magnam voluptatem</li>
<li>nesciunt quod! Earum voluptatibus quaerat dolorem doloribus</li>
<li>molestias ipsum ab, ipsa consectetur laboriosam soluta et</li>
<li>ut doloremque dolore corrupti, architecto iusto beatae.</li>
</ul>
</div>
</div>
</div>
</div>
</template>

Below is what the page will look like:

The script section

First, we’ll fetch the authentication token from local storage in the mounted method.

C++
mounted() {
this.id = this.$route.params.id;
this.product = this.products.find(product => product.id == this.id);
this.category = this.categories.find(category => category.id == this.product.categoryId);
this.token = localStorage.getItem('token');
}

Next, we’ll define the function. We’ll hit the API, baseurl/wishlist/add and send the token in the URL parameter.

We’ll send the productId in the request body. Although, we can send the whole product JSON as a request body, sending id is sufficient.

If we’re successful, we’ll display a sweet alert.

C++
<script>
const axios = require('axios')
import swal from 'sweetalert';
export default {
data(){
return {
product : {},
category : {},
id : null,
quantity: 1
}
},
props : ["baseURL","products", "categories"],
methods:{
addToWishList(productId){
axios.post(`${this.baseURL}wishlist/add?token=${this.token}`, {
id:productId
}).then((response) => {
if(response.status==201) {
swal({
text: "Added to WishList. Please continue",
icon: "success"
});
}
},(error) =>{
console.log(error)
swal({
text: "Something wrong with add to wishlist",
icon: "error",
closeOnClickOutside: false,
});
});
},
},
mounted() {
this.id = this.$route.params.id;
this.product = this.products.find(product => product.id == this.id);
this.category = this.categories.find(category => category.id == this.product.categoryId);
this.token = localStorage.getItem('token');
}
}
</script>

Success alert

When we click the button, we’ll see an alert stating that the item has been added.

The final code

   ¨( @ ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A3ƒ¸A3ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A±ƒ¸A±ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AZƒ¸A÷ƒ¸A÷ƒ¸AZƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AɃ¸Aÿƒ¸Aÿƒ¸AɃ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Azƒ¸Aþƒ¸Aÿƒ¸Aÿƒ¸Aþƒ¸Azƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A.ƒ¸A߃¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A߃¸A.ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Ašƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Ašƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AHƒ¸Aðƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aðƒ¸AHƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A¸ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A¸ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Afƒ¸Aúƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aúƒ¸Afƒ¸Aƒ¸Aƒ¸Aƒ¸A ƒ¸A҃¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A҃¸A ƒ¸Aƒ¸Aƒ¸Aƒ¸A†ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ|¤?ÿ|¤?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A†ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A7ƒ¸A惸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ‚´Aÿjl9ÿjl9ÿ‚´Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A惸A7ƒ¸Aƒ¸Aƒ¸Aƒ¸A	ƒ¸A¦ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿw“=ÿ_L5ÿ_L5ÿw“=ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A¦ƒ¸A	ƒ¸Aƒ¸Aƒ¸Aƒ¸ARƒ¸Aôƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ€®@ÿfa8ÿ^H5ÿ^H5ÿfa8ÿ€®@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aôƒ¸ARƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿr…<ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿr…<ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aqƒ¸Aüƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ}¦?ÿcW7ÿ^H5ÿ^I5ÿ^I5ÿ^H5ÿcW7ÿ}¦?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aüƒ¸Aqƒ¸Aƒ¸Aƒ¸Aƒ¸A(ƒ¸Aڃ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ·Aÿmv:ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^H5ÿmv:ÿƒ·Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aڃ¸A(ƒ¸Aƒ¸Aƒ¸Aƒ¸A’ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿy›>ÿ`P6ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^H5ÿ`P6ÿy›>ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A’ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A@ƒ¸A샸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ²@ÿii9ÿ]G5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ]G5ÿii9ÿ²@ÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A샸A@ƒ¸Aƒ¸Aƒ¸Aƒ¸A
ƒ¸A±ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹AÿuŽ=ÿ_K5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ_K5ÿuŽ=ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A±ƒ¸A
ƒ¸Aƒ¸Aƒ¸Aƒ¸A]ƒ¸Aøƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ¬@ÿe]7ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^J5å^J5å^I5ÿ^I5ÿ^I5ÿ^H5ÿe]7ÿ¬@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aøƒ¸A]ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Ã¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿp;ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ü_L5n_L5n^I5ü^I5ÿ^I5ÿ^I5ÿ^I5ÿp;ÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Ã¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A}ƒ¸Aþƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ|¢?ÿbT6ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ_J5ÁaP6aP6_J5Á^I5ÿ^I5ÿ^I5ÿ^H5ÿbT6ÿ|¢?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aþƒ¸A}ƒ¸Aƒ¸Aƒ¸Aƒ¸A/ƒ¸AჸAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ‚µAÿlq9ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ó_L5P^H5^H5_L5P^I5ó^I5ÿ^I5ÿ^I5ÿ^H5ÿlq9ÿ‚µAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸AჸA/ƒ¸Aƒ¸Aƒ¸Aœƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ„ºAÿx–=ÿ`N6ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ_K5¤bT7aQ6aQ6bT7_K5¤^I5ÿ^I5ÿ^I5ÿ^I5ÿ`N6ÿx–=ÿ„ºAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aœƒ¸Aƒ¸AWƒ¸Aðƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ€¯@ÿgd8ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^J5å`M65_K5jf9jf9_K5`M65^J5å^I5ÿ^I5ÿ^I5ÿ^H5ÿgd8ÿ€¯@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aðƒ¸AWƒ¸Ašƒ¸Aȃ¸Aƃ¸Aƃ¸AƄºAÆv‘=Æ_K5Æ^I5Æ^I5Æ^I5Æ^I5É_K5uha9bS7bS7ha9_K5u^I5É^I5Æ^I5Æ^I5Æ_K5Æv‘=ƄºAƃ¸Aƃ¸Aƃ¸Aƃ¸Aȃ¸Ašƒ¸Aƒ¸A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¹A
lr:
\D4
^I5
^I5
^I5
^I5
`M6^H5^H5`M6^I5
^I5
^I5
^I5
\D4
lr9
ƒ¹A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¸Aÿÿÿÿÿÿÿÿÿþÿÿü?ÿÿü?ÿÿøÿÿøÿÿðÿÿàÿÿàÿÿÀÿÿÀÿÿ€ÿÿÿÿÿþþü?ü?øððààÀÀ€€€ÀÀàÿÿÿÿ
Final code