Product not found, CANNOT_ADD_CART

Hi all,

Hope somebody can help me with trying to add a product to my cart. My current code is as follows:

$w(‘#shoppingCartIcon1’).addToCart(“u6e5657h-d2g9-c98b-590d-16236cfq8e98”, 1, {
“customTextFields”: [ {
“title”: “For whom?”,
“value”: “John”
}],
} )
.then( () => {
console.log(“Product added”);
} )
. catch ( (error) => {
console.log(error);
} );
})

The product code I’ve looked up in the product database under Stores\Products and then the attribute inventoryItem. However, the above code results in the following error, though the product id does exist:

FetchError: {“code”:“CANNOT_ADD_CART”,“commandName”:“AddToCart”,“message”:“Product with id u6e5657h-d2g9-c98b-590d-16236cfq8e98 was not found”,“field”:“”}

Your support would be appreciated!

You don’t actually need to put a quantity in your code if you are just wanting the one product to be added to the cart.

As mentioned in the addToCart function info on the Wix CartIcon API.
Use the optional quantity parameter to add more than one product unit to the shopping cart at one time. If quantity is omitted, one product unit will be added.

Add a product to the cart from a button click

$w("#myButton").onClick( () => {
  $w("#myShoppingCartIcon").addToCart("ea77f230-558f-0ba565e8f827")
    .then( () => {
      console.log("Product added");
    } )
    .catch( (error) => {
      console.log(error);
    } );
} );

So, providing you do actually have the correct productID listed for your required product.

Your code should be something like the add product to cart with options example, however simply without the additional 2 for the quantity and the choices for size and the extra custom info lines for the second product.

Add a product to the cart with options

$w('#shoppingCartIcon1').addToCart("ea77f230-558f-0ba565e8f827", 2, {
    "choices": {
      "Size": "Small"
    },
    "customTextFields": [ {
      "title": "Personalization 1",
      "value": "Personalized Text 1"
    },
    {
      "title": "Personalization 2",
      "value": "Personalized Text 2"
    } ]
  } )
  .then( () => {
    console.log("Product added");
  } )
  .catch( (error) => {
    console.log(error);
  } );

So something like this.

$w.onReady(function () {
} );

$w("#myButton").onClick( () => {
$w("#myShoppingCartIcon").addToCart("ea77f230-558f-0ba565e8f827", {
"customTextFields": [ {
"title": "Personalization 1",
"value": "Personalized Text 1"
} ]
} )
.then( () => {
console.log("Product added");
} )
.catch( (error) => {
console.log(error);
} );
} )

@givemeawhisky Thanks for the clarification.

With your code I get the following: FetchError: {“code”:“ERROR_INVALID_COMMAND_NAME”,“commandName”:“AddCartItemCommand”,“message”:“”,“field”:“”}

Any suggestion as to what might be wrong?

@givemeawhisky , would you have any suggestions to resolve my issue?

In my case it was because the product options variantName wasn’t set.

{ choices: { 'Size': productSelectedOption.variantName }

I was parsing the product option as an object not the name.

Hope this helps someone.

Kind regards

I solve with this:
let product = await createProduct ( myProduct )
. then (( product ) => {
// product created
console . log ( “product created” , product );
productId = String ( product . _id );
})
. catch (( error ) => {
// product not created
console . error ( error )
});

setTimeout (()=>{ 
    $w ( '#shoppingCartIcon1' ). addToCart ( productId ) 
    . then (() => { 
        console . log ( "Product added" ); 
    }) 
    . **catch** (( error ) => { 
        console . log ( error ); 
    }); 
}, 2000 );