createProduct() returns unexplicit error

Hi,

I’m trying to dynamically create a product from code and add it to the store. I have followed the doc from this link but I get a very unexplicit error and have trouble figuring out how to fix it.

Here’s my backend code : backend/productModule.jsw

import wixStoresBackend from 'wix-stores-backend';

export function createConfigProduct(product) {
    return wixStoresBackend.createProduct(product);
}

And simplified client side :


import {createConfigProduct} from 'backend/productModule'

// Custom product object
let config = {
    name: 'Test',
    description: 'Ok',
    price: 0,
    productType: 'physical',
    weight: 0
};

    
let newConfig = await createConfigProduct(config)
   .then(() => {
        console.log('product created - ' + newConfig._id);
   })
   .catch((error) => {
        console.log('Error: ' + (error.stack ? error.stack :                error.message));
        console.log(error);
})

Output :

"{\"message\":\"Bad Request\",\"details\":{}}"
Error: undefined

Has anyone encountered this issue? How to fix it?

Thanks in advance.

Have you tried simply adding the code as in the example shown below in the Wix API for createProduct? Obviously, just take out the product options and variants parts that you do not need, as well as the coupon discount part too.

If the sample code works with your adjustments, then it is your code that needs to be changed to suit.

Plus, I would also look at adding an actual price and weight for it instead of adding it simply as 00, as that might be throwing the code out too as Wix won’t allow zero priced products etc.
https://support.wix.com/en/article/request-offering-free-products-in-wix-stores

/****************
 * backend code *
 ****************/

import wixStoresBackend from 'wix-stores-backend';

export function myBackendFunction(myProduct) {
  return wixStoresBackend.createProduct(myProduct);
}

/********************
 * client-side code *
 ********************/

import {myBackendFunction} from 'backend/myModule';

// ...

const myProduct = {
  "name": "Car Model YO-124",
  "description": "The classiest model in our unique, well-engineered line of luxury cars.",
  "price": 79000,
  "discount": {
    "type": "AMOUNT",
    "value": 5
  },
  "productOptions": {
    "Color": {
      "choices": [{
          "description": "Metallic silver",
          "value": "Silver",
          "mainMedia": "wix:image://light-silver.jpg/file.jpg#originWidth=4896&originHeight=3264",
          "mediaItems": [{
              "src": "wix:image://v1/light-silver-front-view.jpg/file.jpg#originWidth=1000&originHeight=1000",
              "description": "Front view of car",
              "title": "Front view of light silver car",
              "type": "Image"
            },
            {
              "src": "wix:image://v1/light-silver-side-view.jpg/file.jpg#originWidth=1000&originHeight=1000",
              "description": "Side view of car",
              "title": "Side view of light silver car",
              "type": "Image"
            }
          ],
        },
        {
          "description": "Metallic black",
          "value": "Black",
          "mainMedia": "wix:image://black.jpg/file.jpg#originWidth=4896&originHeight=3264"
        }
      ]
    }
  },
  "manageVariants": true,
  "productType": "physical",
  "weight": 1000.0
}

myBackendFunction(myProduct)
  .then( () => {
    // product created 
  } )
  .catch( (error) => {
    // product not created 
  } );

Also, make sure that your inputs are matching what is suggested in the Wix Stores own page about adding physical products like price and description etc.
https://support.wix.com/en/article/adding-a-physical-product-to-wix-stores

I have just tested on a blank page and the example indeed works. After further testing, I found my issue : I’m setting the price of my custom product later in the loop and it seems to be automatically parsed as a string, causing my error.

Small issue in the end but a more detailed error would have been nice.

Free products seem to be allowed actually, they just remain hidden from the client side.

Anyway, thank you for your input.