addToCart is not a function error

there is grand question out there that quite a lot of people sought to figure out

"HOW TO ADD AN ’ ADD TO CART ’ BUTTON TO MY STORE’S DATABASE 'S CUSTOM DYNAMIC PAGE "

I got it work somehow a couple times before 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, create onClick event and apply this code
export function addToshoppingCartIcon_click(event, $w) {  
const productId = $w('dynamicDataset').getCurrentItem()._id;       $w('#shoppingCartIcon1').addToCart(productId, 1 );

and

$w.onReady(function () {

$w("#addToCartButton").onClick((event) => {
let $item = $w.at(event.context);
let selectedProduct = $item('#productsDataset').getCurrentItem();
let productId = selectedProduct._id;
$w('#shoppingCartIcon1').addToCart(productId)
.then(() => {
console.log("add product ${productId} success");
})
.catch((error) => {
console.log(error);
});
});
});

that’s the only time it work, somehow …it did add my product to the cart and jump to cart page but that’s fine

I was so hype…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?

I already know how to setup “standalone add-to-cart-button” which is not what I want.

I don’t want to use the standard wix store page and store widget, I want to use MY designed dynamic page.

the solution to this error is my only objective of this discussion.

Hello,

Are you seeing any errors in the console when running your code? Try to console.log() them and debug your code. This article might help.

I’ve made a test on my site and the following code works for me:

export function button1_click(event) {
let itemObj = $w("#dynamicDataset").getCurrentItem();
console.log(itemObj._id)
$w("#shoppingCartIcon1").addToCart(itemObj._id, 1, { choices: { Color: 'black' } });
}

Please refer to the API reference an the tutorial below. Both of them have code snippets that you can adjust to your needs and implement on your site:

Corvid Tutorial: Adding Multiple Items to the Cart in a Wix Stores Site

Good luck!

thank you very much for your help, however its still no go for me.

it still show

TypeError: $w(...).addToCart is not a function

for full disclosure, I’m still very noob on this matter, so how do I do console.log() to debug it? such as, to put it where?