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.
We'll cover the following...
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:
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.
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.
Success alert
When we click the button, we’ll see an alert stating that the item has been added.