I’m trying to create ‘Add To Cart’ with product options in a repeater, and customers can stay the same page to select options ( Select Weight, Select Grind, quantities, custom text fields, etc. ) from repeater then add to cart. Do I need to add dropdown menus or other elements to the repeater? I tried a few days, but still don’t know how to achieve this.
Any helps would be much appreciated!
I found an answer and try the code below, that works if only products without options.
$w.onReady( function () {
$w(“#button31”).onClick((event) => { let $item = $w.at(event.context); // Get container scope let selectedProduct = $item(‘#dataset1’).getCurrentItem(); let productId = selectedProduct._id;
$w(‘#shoppingCartIcon1’).addToCart(productId)
.then(() => {
// Item added to the shopping cart
console.log(“add product ${productId} success”);
})
. catch ((error) => {
// Catch an error that occurs
console.log(“Error: ${error.stack}”);
});
});
});
I’m also looking for a solution to a similar problem to this. When my products don’t have options then the add to cart button works fine. When they have options I have not found a good way to link a drop down menu to those options, and then let the selection by user inform what is added to the cart.
The above code will only add the product (without any option parameter) and set the quantity to be 1. If you want to add the option parameter see below.
In the above code I am passing the ‘Size’ parameter under ‘choices’ to be ‘Small’. Note that for this code I must have the ‘Size’ variant on my product and one of the variant options should be ‘Small’.
If instead of ‘Size’ I have ‘Color’ as a variant and one of the color variants is ‘Blue’ I will use the below code.