Dynamic "Add To Cart" button

I need help with an Add To Cart button on a dynamic page to make it add the product. Like how the Stores Product Page Works, but in another dynamic page.

1 Like

It is urgent, please!

Hi,
After creating a dynamic page and adding the Products dataset, you should add a button (a regular button), add an onClick event, get the product ID using the getCurrentItem function and use the addToCart function to add it to the cart. Hereā€™s an example of what Iā€™m referring:

export function addToCartBtn_click(event, $w) {
 const productId = $w('#dynamicDataset').getCurrentItem()._id;
    $w("#shoppingCartIcon1").addToCart(productId, 1, { choices: { Color: 'red' } });
}

Good luck,
Tal.

Tal, I canā€™t manage to do it.

I added the onClick event to the button:

export function button15_click(event, $w) {
//Add your code for this event here:
}

Now what do i do from there?

Hey, did you figure this out? Iā€™m having the same problem.

This code does not work.

This is the code I have used -

export function addToshoppingCartIcon_click(event, $w) {
const productId = $w(ā€˜dynamicDatasetā€™).getCurrentItem()._id;
$w(ā€˜#shoppingCartIconā€™).addToCart(productId, 1, );
}

Once clicked, nothing happens, no warning, no add to cart.

Could you please help? Iā€™m trying to create a dynamic page with add to cart button, based on the product shown on the dynamic page - but nothing seems to be working.

is there any solution?

I got it work somehow a couple times then no go again

what I did was:

  1. I created dynamic page from the original Wix storeā€™s ā€œproductsā€ dataset
  2. I build regular button and apply the code without the product option part and hit preview

thatā€™s the only time it work, somehow
it did add my product to the cart and jump to check-out page but thatā€™s fine

then it didnā€™t work again ā€¦somehow

on the console it show the error as: addTocart is not a function
which is odd because when it compile it can work and i did find the function but it become not when preview.

can anyone figure something from what I told?

Old post being closed, please add a new post and refer back to this in a link if needed, rather than bumping an old thread from 2018.