Adding an add to cart button on a custom dynamic product page

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 );
});
} );

:flushed:
Thanks,
Laura

@ahmadnasriya hi :smirk: I see your a master and i’ve seen you help some people in a way I was able to understand… any ideas? I’ve spent sooo long. :flushed:

I need the same help. 3 of my product pages did not need the upload image button. I created individual pages for those products but can’t add them to cart. Add to cart button goes to an empty cart page. Any help will be appreciated.

Laura, did you receive help with this? I tried both of your codes above but unfortunately they did not work for me.

Hey Sales, yeah I did. I gave up and paid for some help.
This was the code. Works now. Hope that helps!

$w . onReady ( function () {

});

export function addtocartbutton_click ( event ) {

let currentItem = $w ( “#dynamicDataset” ). getCurrentItem ();
let productId = currentItem . _id ;

$w ( '#shoppingCartIcon2' ). addToCart ( productId ,  1 ) 

. then ( () => {
console . log ( ${ productId } added successfully );
} )
. catch ( ( error ) => {
console . log ( error );
} );
}

export function addtocartbutton2_click ( event ) {

let currentItem = $w ( “#dynamicDataset” ). getCurrentItem ();
let productId = currentItem . _id ;

$w ( '#shoppingCartIcon2' ). addToCart ( productId ,  1 ) 

. then ( () => {
console . log ( ${ productId } added successfully );
} )
. catch ( ( error ) => {
console . log ( error );
} );
}