On Product Created and On Product Updates

In Wix Studio i am trying to use onProductCreated() to add the basic product information to CMS, the CMS permission setup as all can view, add, and update to avoid any problem and I followed Velo document, but the code is not work, any body can help on that. kindly advise.

import wixStoresBackend from "wix-stores-backend";
import wixData from 'wix-data';

export function wixStores_onProductCreated(event) {
  let productId = event._id
  let productName = event.name;
  let productDescription = event.description;

  let toInsert = {
  "title": productName,
  "product": productId,
  "description": productDescription,
};

wixData
  .insert("ProductAdditionalInfo", toInsert)
  .then((item) => {
    console.log(item); //see item below
  })
  .catch((err) => {
    console.log(err);
  });
}

Allowing all to add/edit/view from a CMS collection would not avoid trouble, it’ll only invite it - Now anyone can just modify your contents to their whim. Instead, use elevate or surpassAuth so that the backend functions are executed with the server’s limitless permissions

The ProductCreatedEvent does not contain a property called ‘description’

As for the actual cause of your problem, I do not know, sorry

1 Like

Hey there,

You need to put this code inside the events.js file in the backend.

Check out this post that shows how to create the events.js code file:

Also, as Dean mentioned, the returned event object does not include the product description, so you might want to remove that before testing it out.