I am trying to add a custom add-to-cart code to the submit button.
The submit button is for a custom form that takes custom sizes of the current product.
On submitting the form, the current product is automatically added to the cart.
My current code works well with products with no variants, but when products with variants are added, it opens an empty mini cart.
Website: https://www.deepeesjodhpur.com/
import wixWindow from 'wix-window';
import { cart } from 'wix-stores'
let receivedData = wixWindow.lightbox.getContext();
$w.onReady(function () {
init();
$w('#datasettorso').onBeforeSave(() => {
console.log("Continuing save");
$w('#datasettorso').setFieldValue("product", receivedData.id);
return true;
});
$w('#datasetbottoms').onBeforeSave(() => {
console.log("Continuing save");
$w('#datasetbottoms').setFieldValue("product", receivedData.id);
return true;
});
$w('#datasetboth').onBeforeSave(() => {
console.log("Continuing save");
$w('#datasetboth').setFieldValue("reference", receivedData.id);
return true;
});
});
function init() {
let option = [
{ "label": receivedData.name, "value": receivedData.id }
];
}
export function addToCart_click(event) {
let product = $w('#datasettorso').getCurrentItem();
let idProduct = product.product;
$w('#shoppingCartIcon2').addToCart(idProduct)
.then(() => {
console.log("Product Added");
cart.showMiniCart();
})
.catch((error) => {
console.log(error);
});
}
export function addToCart2_click(event) {
let product = $w('#datasetbottoms').getCurrentItem();
let idProduct = product.product;
$w('#shoppingCartIcon2').addToCart(idProduct)
.then(() => {
console.log("Product Added");
cart.showMiniCart();
})
.catch((error) => {
console.log(error);
});
}
export function addToCart3_click(event) {
let product = $w('#datasetboth').getCurrentItem();
let idProduct = product.reference;
$w('#shoppingCartIcon2').addToCart(idProduct)
.then(() => {
console.log("Product Added");
cart.showMiniCart();
})
.catch((error) => {
console.log(error);
});
}