Creates a new product.
The createProduct()
function receives a ProductInfo
object and returns a Promise that resolves to a Product
object when the product has been created.
Creating a product is the first step in the process of enabling visitors to buy your products. After you create a product, you can add choices and variants to the product.
Notes:
"1000"
milliseconds (1 second) before calling wix-stores.cart.addProducts()
. While this slows down the operation slightly, it also ensures proper retrieval of the newly created product before adding it to the cart.function createProduct(productInfo: ProductInfo): Promise<Product>;
Information for the product being created.
/*******************************
* Backend code - products.jsw *
*******************************/
import wixStoresBackend from "wix-stores-backend";
export function createProduct(myProduct) {
return wixStoresBackend.createProduct(myProduct);
}
/*************
* Page code *
*************/
import { createProduct } from "backend/products";
// ...
const myProduct = {
name: "Colombian Arabica",
description: "The best organic coffee that Colombia has to offer.",
price: 35,
pricePerUnitData: {
totalQuantity: 100,
totalMeasurementUnit: "G",
baseQuantity: 1,
baseMeasurementUnit: "G",
},
sku: "Colombian-001",
visible: true,
discount: {
type: "AMOUNT",
value: "5",
},
productOptions: {
Weight: {
choices: [
{
value: "250g",
description: "250g",
},
{
value: "500g",
description: "500g",
},
],
},
},
manageVariants: true,
productType: "physical",
weight: 250,
ribbon: "Organic",
brand: "Coffee Company",
seoData: {
tags: [
{
type: "title",
children: "Colombian Arabica",
custom: false,
disabled: false,
},
{
type: "meta",
props: {
name: "description",
content: "The best organic coffee that Colombia has to offer.",
},
custom: false,
disabled: false,
},
],
},
};
createProduct(myProduct)
.then((product) => {
// product created
const productId = product._id;
const description = product.description;
})
.catch((error) => {
// product not created
console.error(error);
});
/* Example of returned product object
*
* {
* "_id": "3ceafef8-7f07-413b-8f72-0229049fab19",
* "_updatedDate": "Mon Feb 15 2021 17:03:18 GMT+0200 (Israel Standard Time)",
* "name": "Colombian Arabica",
* "description": "The best organic coffee that Colombia has to offer.",
* "mainMedia": "wix:image://v1/614034_103e8f4ab0ae4536a38b319d3eb437ed~mv2.png/missing-media.png#originWidth=500&originHeight=500",
* "mediaItems": [],
* "ribbon": "Organic",
* "brand": "Coffee Company"
* "currency": "USD",
* "price": 35,
* "discountedPrice": 30,
* "formattedPrice": "$35.00",
* "formattedDiscountedPrice": "$30.00",
* "pricePerUnit": 0.3,
* "formattedPricePerUnit": "$0.30",
* "pricePerUnitData": {
* "totalQuantity": 100,
* "totalMeasurementUnit": "G",
* "baseQuantity": 1,
* "baseMeasurementUnit": "G"
* },
* "inventoryItemId": "c3150107-80f8-bec4-708d-fdd6fb6054e6",
* "discount": {
* "type": "AMOUNT",
* "value": 5
* },
* "trackInventory": false,
* "inStock": true,
* "additionalInfoSections": [],
* "productOptions": {
* "Weight": {
* "optionType": "drop_down",
* "name": "Weight",
* "choices": [
* {
* "value": "250g",
* "description": "250g",
* "inStock": true,
* "visible": true,
* "mainMedia": "null",
* "mediaItems": []
* },
* {
* "value": "500g",
* "description": "500g",
* "inStock": true,
* "visible": true,
* "mainMedia": "null",
* "mediaItems": []
* }
* ]
* }
* },
* "productPageUrl": "/product-page/colombian-arabica",
* "customTextFields": [],
* "manageVariants": true,
* "productType": "physical",
* "slug": "colombian-arabica",
* "seoData": {
* "tags": [
* {
* "type": "title",
* "children": "Colombian Arabica",
* "custom": false,
* "disabled": false
* },
* {
* "type": "meta",
* "props": {
* "name": "description",
* "content": "The best organic coffee that Colombia has to offer."
* },
* "children": "",
* "custom": false,
* "disabled": false
* }
* ]
* },
* "variants": [
* {
* "_id": "33599a3c-73a6-4532-9786-8d70f096d669",
* "choices": {
* "Weight": "250g"
* },
* "variant": {
* "currency": "USD",
* "price": 35,
* "discountedPrice": 30,
* "pricePerUnit": 0.3,
* "formattedPrice": "$35.00",
* "formattedDiscountedPrice": "$35.00",
* "formattedPricePerUnit": "$0.30",
* "weight": 250,
* "sku": "Colombian-001",
* "visible": true
* }
* },
* {
* "_id": "4cf12a28-d493-47b1-8475-f45b043ac683",
* "choices": {
* "Weight": "500g"
* },
* "variant": {
* "currency": "USD",
* "price": 35,
* "discountedPrice": 30,
* "pricePerUnit": 0.3,
* "formattedPrice": "$35.00",
* "formattedDiscountedPrice": "$35.00",
* "formattedPricePerUnit": "$0.30",
* "weight": 250,
* "sku": "Colombian-001",
* "visible": true
* }
* }
* ]
* }
*
*/
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.