Hello. I’ve created a dynamic page for my products and I want to be able to add a button with the ‘add to cart’ functionality. So when clicked, I want the current item to be added to the cart. With the code i’ve pasted below… nothings happening.
I’ve spent so many hours on this and seen some posts around the same thing but still, i’m stuck.
My coding knowledge isn’t great but i’ve given it a go. Here’s where i’m at:
import wixData from ‘wix-data’ ;
import wixStores from ‘wix-stores’ ;
$w . onReady ( function () {
$w ( “#dynamicDataset” ). onReady
});
// Add the item to the shopping cart.
export function button5 ( event ) {
let currentProduct = $w ( ‘#dynamicDataset’ ). getCurrentItem ();
console . log ( “inside button click” );
console . log ( currentProduct . _id )
$w ( ‘#shoppingCartIcon2’ ). addToCart ( currentProduct . _id , 1 )
. then (() => {
// Item added to the shopping cart
console . log ( “add product ${productId} success” );
})
. catch (( error ) => {
// Catch an error that occurs
console . log ( error );
});
}
Have also tried this:
import wixData from ‘wix-data’ ;
import wixStores from ‘wix-stores’ ;
$w . onReady ( function () {
$w ( “#dynamicDataset” ). onReady
});
// Add the item to the shopping cart
$w ( “#button5” ). onClick ( () => {
$w ( “#myShoppingCartIcon2” ). addToCart (( currentProduct . _id , 1 ))
. then (() => {
// Item added to the shopping cart
console . log ( “add product ${productId} success” );
})
. catch (( error ) => {
// Catch an error that occurs
console . log ( error );
});
} );
![]()
Thanks,
Laura