I would like to be able to add Wix products that you can manage in the back office via Velo (code). Not via the UI in the dashboard. This would be faster for me and I can add products automatically or directly in the website via button. Is there the possibility to do this ? Thanks for answers.
https://www.wix.com/velo/reference/wix-stores-backend/createproduct
Here is the full flow for creating a new product.
This example shows how to create a product, add it to a collection, and add an image to an option.
You can add a new product by adding this code to the input elements on your page.
/*******************************
- Backend code - products.jsw *
*******************************/
import wixStoresBackend from âwix-stores-backendâ;
export function createProduct(product) {
return wixStoresBackend.createProduct(product);
}
export function addProductsToCollection(product, collection) {
return wixStoresBackend.addProductsToCollection(product, collection);
}
export function addProductMedia(product, mediaData) {
return wixStoresBackend.addProductMedia(product, mediaData);
}
// Add appropriate input fields for the red fields in the page code.
// Replace the input fields with the product information written in red by editing the content to code it.
/*************
- Page code *
*************/
import {
createProduct,
addProductsToCollection,
addProductMedia
} from âbackend/productsâ;
$w.onReady(function () {
$w(â#addProductButtonâ).onClick(async () => {
const product = {
"name": **"Pants"** ,
"description": **"Straight-legged skinny jeans."** ,
"price": **70** ,
"productOptions": {
"Color": {
"choices": [{
"description": " **Black** ",
"value": " **Black** "
},
{
"description": " **Blue** ",
"value": " **Blue** "
}
]
}
},
"manageVariants": **true** ,
"productType": " **physical** ",
}
try {
let newProduct = await createProduct(product);
// The product was created. Now assign it
// to a collection. Convert the product ID
// to an array.
const newProducts = [newProduct._id];
const collectionId = // get collection ID
addProductsToCollection(collectionId, newProducts);
// The product was assigned to a collection.
// Now let's add media.
const option = // get option, such as "Color." Can be undefined.
const choice = // get choice, such as "Black." Can be undefined.
const src = // get src
const mediaData = [{ // add media item to the object
src
}]
// If a choice and option are defined,
// addProductMedia() adds media to choice
if (choice !== "" && option !== "") {
mediaData[0].choice = {
choice,
option
}
}
addProductMedia(newProduct, mediaData);
}
catch (err) {
// handle the error
}
});
})
//*************************************************************
/* Full product object:
*
- {
-
"_ id": "9a10ada3-...-111c6babc398",
-
"_updatedDate": "2020-12-30T14:56:45.626Z",
-
"name": "Pants",
-
"description": "Straight-legged skinny jeans.",
-
"mainMedia": "wix:image://v1/1c...1111cc111~mv2.jpg#originWidth=50&originHeight=50",
-
"mediaItems": [],
-
"currency": "USD",
-
"price": 70,
-
"discountedPrice": 66,
-
"formattedPrice": "$100.00",
-
"formattedDiscountedPrice": "$66.00",
-
"inventoryItemId": "65ef525c-...-43c67",
-
"discount": {
-
"type": "AMOUNT",
-
"value": 5
-
},
-
"trackInventory": false,
-
"inStock": true,
-
"additionalInfoSections": [ ],
-
"productOptions": {
-
"Color": {
-
"optionType": "drop_down",
-
"name": "Color",
-
"choices": [
-
{
-
"value": "Black",
-
"description": "Black",
-
"inStock": true,
-
"visible": true,
-
"mainMedia": "wix:image://v1/1c111c1...1111cc111~mv2.jpg/1c411c1...1111cc111~mv2.jpg#originWidth=50&originHeight=50",
-
"mediaItems": [
-
{
-
"id" : 0d26de75...5379.jpg,
-
"src" : wix:image://v1/0d26de75...5379.jpg/file.jpg#originWidth=1000&originHeight=1776,
-
"description" : ,
-
"title" : ,
-
"type" : Image
-
}
-
]
-
},
-
{
-
"value": "Blue",
-
"description": "Blue",
-
"inStock": true,
-
"visible": true,
-
"mainMedia": null,
-
"mediaItems": [ ]
-
}
-
]
-
}
-
},
-
"productPageUrl": "/product-page/pants",
-
"customTextFields": [ ],
-
"manageVariants": true,
-
"productType": "physical",
-
"slug": "pants",
-
"collections": [e22737a6-...-3fcfd5a"]
-
"variants": [
-
{
-
"_id": "00000000-...-9aed0d5c3297",
-
"choices": {
-
"Color": "Black"
-
},
-
"variant": {
-
"currency": "USD",
-
"price": 70,
-
"discountedPrice": 66,
-
"formattedPrice": "$70.00",
-
"formattedDiscountedPrice": "$66.00",
-
"weight": 1,
-
"sku": "",
-
"visible": true
-
}
-
},
-
{
-
"_id": "00000000-...-9aed0d5c3297",
-
"choices": {
-
"Color": "Blue"
-
},
-
"variant": {
-
"currency": "USD",
-
"price": 70,
-
"discountedPrice": 66,
-
"formattedPrice": "$70.00",
-
"formattedDiscountedPrice": "$66.00",
-
"weight": 1,
-
"sku": "",
-
"visible": true
-
}
-
}
-
]
-
}
- }
*/