Add to Cart from Custom database

Hello!
I am trying to make a website page where the customer can modify the product’s dimensions, which will affect the product’s price. I am using a custom database to add the customized product, but I am unable to add products from the database to the cart.
This is the code I have written so far.

let itemObj = $w("#Dataset").
const products = [{
                "productId": itemObj._id,
                "quantity": itemObj.quantity,
            }];
           
           cart.addProducts(products)
                .then((updatedCart) => {
                    // Products added to cart
                    const cartId = updatedCart._id;
                    const cartLineItems = updatedCart.lineItems;
                    console.log(updatedCart);
                    console.log("Products added");
                })
                .catch((error) => {
                    // Products not added to cart
                    console.error(error);
                });

This is not giving me any error, but the product Lso doesn’t get added to the cart.
Can someone tell me how to solve this?

Thanks

When you console.log the products object, is there anything in there?

Yup I can see the product id. But that product is added to a custom collection called “Dataset” instead of the inventory “Products”. I am unable to add a product from collection to the cart using velo code.

Are you using the custom catalog SPI? It sounds like this is what you could need.

https://www.wix.com/velo/reference/spis/getting-started/ecommerce-catalog

Hey
Thank you for the reply. I tried using SPI but how to make sure that when I add product to the cart, this SPI is getting called? I have added code into the custom extensions but not sure if it’s getting called before a product is added to cart

When configuring the custom catalog, you have to. make sure to configure the appId to point to your custom collection. That’s likely the missing piece.

See this tutorial, it’s no. 10 in step 1 - it tells you how to get and then set the new app id for the custom catalog. https://support.wix.com/en/article/velo-tutorial-ecommerce-catalog-custom-extension-beta

Once it’s pointing to your catalog, the add to cart functionality is automatically pointing to those items instead of the regular product collection

I tried that but the api id was only visible the first time I added the extension and published the site. When publishing the site after that, it wasn’t visible. So I am not sure what happened. The app store also doesn’t show the custom catalog.

That is correct that the appID is only available the first time (for now, this is still a beta functionality so things could change). This is why the step says to save this ID. I will have to ask how to retrieve it another way, I’m not sure at the moment.

You also need the appID again for the testing addToCurrentCart as shown here: https://www.wix.com/velo/reference/wix-ecom-backend/currentcart/addtocurrentcart?branch=autodocs-wix-ecom-backend

specifically, when testing a custom catalog you must add appID here in the line items

I will get back to you about how to get the appID if you have not saved it after the initial step and see if having that helps get this working

Hey. Did you solved this issue? I am facing the same problem.

Tks