cart.addProducts();

Hi, I know there are many posts about this but none of them answered my question. I have tried both cart.addProducts() and the deprecated addTocart() and I have not been able to successfully add any products to a cart. Here’s the lines of code that I’m trying to use to get to work (note this isn’t the exact format on my product page that there’s a lot more going on:

import { cart } from ‘wix-stores’;

let currProduct = null;

$w(“#productPage”).getProduct()
.then((product)=>{
// Enable global access to product object
currProduct = product;
// configure the page with product info
.catch((err)=>{
console.log(err)
})

let cartItem
let variants = currProduct.variants

// color and size variables are updated between declaration and the for loop. The below For Loop works. Excluding bits for brevity.
let color = null;
let size = null;

for (let i=0; i < variants.length;i++) {
if (variants[i].choices.Size == size && variants[i].choices.Color == color) {
// the array with an object inside of it is how wix wants me to format the input. when logging the value, productId is a
// valid product Id from inventory. I have tried both with quotes and without (i.e. productId/“productId”/‘productId’)
cartItem = [{productId: variants[i]._id, quantity: 1}];
}
}

cart.addProducts(cartItem)
.then((updatedCart) => {
// This returns an empty array and nothing is added to the Cart
console.log(updatedCart._id, updatedCart.lineItems);
})
.catch((error) => {
// Products not added to cart
console.error(error);
});

// END OF CODE SNIPPET

For further context, this is not a new site. I am trying to update and existing site that has an active cart and product page. I’m attempting to pull data from the existing product element and make a custom product page from that data.

Did you try this with a timeout? Perhaps the item is not available to be retrieved when you are calling it