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.