Question:
How can I create an add to cart button, that when users click, the product is added to their cart and they are taken to the cart page?
Product:
[Which editor or feature is your question most relevant to? Wix Studio
What are you trying to achieve:
I am trying to create “Add to cart” buttons that work. Buttons that when clicked take the user to the cart page, with the product in their cart.
What have you already tried:
I tried the following code to add a delay to the button, but it isn’t working consistently.
setTimeout(function() {
// Your code to be executed after the delay of 2 seconds
}, 2000);
Below is a sample code snippet to demonstrate how you might implement this:
import wixStoresBackend from 'wix-stores-backend';
export function addToCartWithDelay(product) {
// Set a delay time in milliseconds (e.g., 2000 milliseconds = 2 seconds)
const delayTime = 2000;
setTimeout(function () {
// Code to add the item to the cart
wixStoresBackend.addToCart(product)
.then((response) => {
// Handle the successful addition to the cart
console.log("Product added to cart:", response);
})
.catch((error) => {
// Handle any errors during the addition
console.error("Error adding product to cart:", error);
});
}, delayTime);
}
Additional information:
The live website is zeroexperts.co. Sometimes when you click the buttons, the product is added to cart and you are taken to the cart page. However this is not consistent.
Wix Support have said that this requires “Velo” which I don’t know how to use unfortunately.