Search.../

createProduct( )

Creates a new product.

Description

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:

  • If you create a product immediately before adding it to the cart, we suggest setting a timeout for "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.
  • Do not pass important information from client-side code. Doing so opens a vulnerability that a malicious user can exploit to change information, such as a buyer’s personal details (address, email, etc.) or product price information. To learn more about how to keep your code secure, see Security Considerations When Working with Wix Code.

Syntax

function createProduct(productInfo: ProductInfo): Promise<Product>

createProduct Parameters

NAME
TYPE
DESCRIPTION
productInfo
ProductInfo

Information for the product being created.

Returns

Fulfilled - The created product. Rejected - Error message.

Return Type:

Promise<Product>
NAME
TYPE
DESCRIPTION
_id
string

Product ID.

_updatedDate
string

Date product was last updated.

name
string

Product name.

description
string

Product description.

mainMedia
string

Main product media item URL (wix:image or https).

mediaItems
Array<MediaItem>

List of product media items.

sku
string

Product stock keeping unit value. Must be unique.

ribbons
Array<ProductRibbon>

Deprecated. Use ribbon instead.

currency
string

Product currency.

price
number

Product price. The price must be greater than its discount. The product price is propagated to the product's newly-created variants. Product variants whose prices have been updated directly are not affected by changes to the product price.

discountedPrice
number

Discounted product price.

formattedPrice
string

Product price formatted with the currency.

formattedDiscountedPrice
string

Discounted product price formatted with currency symbol.

inventoryItemId
string

ID for the inventory item.

discount
ProductDiscount

Product discount.

trackInventory
boolean

Indicates whether inventory is tracked for the product.

inStock
boolean

Indicates whether the product is in stock.

quantityInStock
number

Number of units currently in stock.

additionalInfoSections
Array<ProductAdditionalInfoSection>

Additional product information sections.

productOptions
ProductOptions

All the available options for a store product.

productPageUrl
string

Product page relative URL.

manageVariants
boolean

Indicates whether product variants are managed. Can be set to true only if the product has options. Once set to true, can be reset to false only if no variants exist. Use getProductVariants() to check if variants exist. You cannot set manageVariants to true if more than 300 variants are defined.

customTextFields
Array<ProductCustomTextFields>

List of product customization fields.

productType
string

Product type.

slug
string

Product slug.

weight
string

Product weight.

visible
boolean

Whether the product is visible to site visitors and appears in Content Manager collections.

variants
Array<VariantItem>

Product variants.

seoData
SeoData

Custom SEO data for the product. Learn more about SEO.

pricePerUnitData
pricePerUnitData

Details of the product's price per unit.

pricePerUnit
number

Price per unit.

formattedPricePerUnit
string

Price per unit formatted with currency symbol.

ribbon
string

Product ribbon. Used to highlight relevant information about a product. For example, "Sale", "New Arrival", "Sold Out".

brand
string

Product brand. Including a brand name can help improve your site’s visibility on search engines.

Related Content:

Was this helpful?

Create a new product

Copy Code
1/*******************************
2 * Backend code - products.jsw *
3 *******************************/
4
5import wixStoresBackend from 'wix-stores-backend';
6
7export function createProduct(myProduct) {
8 return wixStoresBackend.createProduct(myProduct);
9}
10
11/*************
12 * Page code *
13 *************/
14
15import { createProduct } from 'backend/products';
16
17// ...
18
19const myProduct = {
20 "name": "Colombian Arabica",
21 "description": "The best organic coffee that Colombia has to offer.",
22 "price": 35,
23 "pricePerUnitData": {
24 "totalQuantity": 100,
25 "totalMeasurementUnit": "G",
26 "baseQuantity": 1,
27 "baseMeasurementUnit": "G"
28 },
29 "sku": "Colombian-001",
30 "visible": true,
31 "discount": {
32 "type": "AMOUNT",
33 "value": "5"
34 },
35 "productOptions": {
36 "Weight": {
37 "choices": [{
38 "value": "250g",
39 "description": "250g"
40 },
41 {
42 "value": "500g",
43 "description": "500g"
44 }
45 ]
46 }
47 },
48 "manageVariants": true,
49 "productType": "physical",
50 "weight": 250,
51 "ribbon": "Organic",
52 "brand": "Coffee Company",
53 "seoData": {
54 "tags": [{
55 "type": "title",
56 "children": "Colombian Arabica",
57 "custom": false,
58 "disabled": false
59 },
60 {
61 "type": "meta",
62 "props": {
63 "name": "description",
64 "content": "The best organic coffee that Colombia has to offer."
65 },
66 "custom": false,
67 "disabled": false
68 }
69 ]
70 }
71}
72
73createProduct(myProduct)
74 .then((product) => {
75 // product created
76 const productId = product._id
77 const description = product.description
78 })
79 .catch((error) => {
80 // product not created
81 console.error(error)
82 });
83
84/* Example of returned product object
85 *
86 * {
87 * "_id": "3ceafef8-7f07-413b-8f72-0229049fab19",
88 * "_updatedDate": "Mon Feb 15 2021 17:03:18 GMT+0200 (Israel Standard Time)",
89 * "name": "Colombian Arabica",
90 * "description": "The best organic coffee that Colombia has to offer.",
91 * "mainMedia": "wix:image://v1/614034_103e8f4ab0ae4536a38b319d3eb437ed~mv2.png/missing-media.png#originWidth=500&originHeight=500",
92 * "mediaItems": [],
93 * "ribbon": "Organic",
94 * "brand": "Coffee Company"
95 * "currency": "USD",
96 * "price": 35,
97 * "discountedPrice": 30,
98 * "formattedPrice": "$35.00",
99 * "formattedDiscountedPrice": "$30.00",
100 * "pricePerUnit": 0.3,
101 * "formattedPricePerUnit": "$0.30",
102 * "pricePerUnitData": {
103 * "totalQuantity": 100,
104 * "totalMeasurementUnit": "G",
105 * "baseQuantity": 1,
106 * "baseMeasurementUnit": "G"
107 * },
108 * "inventoryItemId": "c3150107-80f8-bec4-708d-fdd6fb6054e6",
109 * "discount": {
110 * "type": "AMOUNT",
111 * "value": 5
112 * },
113 * "trackInventory": false,
114 * "inStock": true,
115 * "additionalInfoSections": [],
116 * "productOptions": {
117 * "Weight": {
118 * "optionType": "drop_down",
119 * "name": "Weight",
120 * "choices": [
121 * {
122 * "value": "250g",
123 * "description": "250g",
124 * "inStock": true,
125 * "visible": true,
126 * "mainMedia": "null",
127 * "mediaItems": []
128 * },
129 * {
130 * "value": "500g",
131 * "description": "500g",
132 * "inStock": true,
133 * "visible": true,
134 * "mainMedia": "null",
135 * "mediaItems": []
136 * }
137 * ]
138 * }
139 * },
140 * "productPageUrl": "/product-page/colombian-arabica",
141 * "customTextFields": [],
142 * "manageVariants": true,
143 * "productType": "physical",
144 * "slug": "colombian-arabica",
145 * "seoData": {
146 * "tags": [
147 * {
148 * "type": "title",
149 * "children": "Colombian Arabica",
150 * "custom": false,
151 * "disabled": false
152 * },
153 * {
154 * "type": "meta",
155 * "props": {
156 * "name": "description",
157 * "content": "The best organic coffee that Colombia has to offer."
158 * },
159 * "children": "",
160 * "custom": false,
161 * "disabled": false
162 * }
163 * ]
164 * },
165 * "variants": [
166 * {
167 * "_id": "33599a3c-73a6-4532-9786-8d70f096d669",
168 * "choices": {
169 * "Weight": "250g"
170 * },
171 * "variant": {
172 * "currency": "USD",
173 * "price": 35,
174 * "discountedPrice": 30,
175 * "pricePerUnit": 0.3,
176 * "formattedPrice": "$35.00",
177 * "formattedDiscountedPrice": "$35.00",
178 * "formattedPricePerUnit": "$0.30",
179 * "weight": 250,
180 * "sku": "Colombian-001",
181 * "visible": true
182 * }
183 * },
184 * {
185 * "_id": "4cf12a28-d493-47b1-8475-f45b043ac683",
186 * "choices": {
187 * "Weight": "500g"
188 * },
189 * "variant": {
190 * "currency": "USD",
191 * "price": 35,
192 * "discountedPrice": 30,
193 * "pricePerUnit": 0.3,
194 * "formattedPrice": "$35.00",
195 * "formattedDiscountedPrice": "$35.00",
196 * "formattedPricePerUnit": "$0.30",
197 * "weight": 250,
198 * "sku": "Colombian-001",
199 * "visible": true
200 * }
201 * }
202 * ]
203 * }
204 *
205 */
Full flow for creating a new product

This example demonstrates how to create a product, add it to a collection, and add images to an option.

Copy Code
1/*******************************
2 * Backend code - products.jsw *
3 *******************************/
4
5import wixStoresBackend from 'wix-stores-backend';
6
7export function createProduct(product) {
8 return wixStoresBackend.createProduct(product);
9}
10
11export function addProductsToCollection(product, collection) {
12 return wixStoresBackend.addProductsToCollection(product, collection);
13}
14
15export function addProductMedia(product, mediaData) {
16 return wixStoresBackend.addProductMedia(product, mediaData);
17}
18
19/*************
20 * Page code *
21 *************/
22
23import {
24 createProduct,
25 addProductsToCollection,
26 addProductMedia
27} from 'backend/products';
28
29$w.onReady(function () {
30 $w("#addProductButton").onClick(async () => {
31
32 const product = {
33 "name": "Pants",
34 "description": "Straight-legged skinny jeans.",
35 "price": 70,
36 "productOptions": {
37 "Color": {
38 "choices": [{
39 "description": "Black",
40 "value": "Black"
41 },
42 {
43 "description": "Blue",
44 "value": "Blue"
45 }
46 ]
47 }
48 },
49 "manageVariants": true,
50 "productType": "physical",
51 }
52
53 try {
54 let newProduct = await createProduct(product);
55
56 // The product was created. Now assign it
57 // to a collection. Convert the product ID
58 // to an array.
59 const newProducts = [newProduct._id];
60 const collectionId = // get collection ID
61
62 addProductsToCollection(collectionId, newProducts);
63
64 // The product was assigned to a collection.
65 // Now let's add media.
66 const option = // get option, such as "Color." Can be undefined.
67 const choice = // get choice, such as "Black." Can be undefined.
68 const src = // get src
69
70 const mediaData = [{ // add media item to the object
71 src
72 }]
73
74 // If a choice and option are defined,
75 // addProductMedia() adds media to choice
76 if (choice !== "" && option !== "") {
77 mediaData[0].choice = {
78 choice,
79 option
80 }
81 }
82
83 addProductMedia(newProduct, mediaData);
84 }
85 catch (err) {
86 // handle the error
87 }
88 });
89})
90
91/* Full product object:
92 *
93 * {
94 * "_id": "9a10ada3-...-111c6babc398",
95 * "_updatedDate": "2020-12-30T14:56:45.626Z",
96 * "name": "Pants",
97 * "description": "Straight-legged skinny jeans.",
98 * "mainMedia": "wix:image://v1/1c...1111cc111~mv2.jpg#originWidth=50&originHeight=50",
99 * "mediaItems": [],
100 * "currency": "USD",
101 * "price": 70,
102 * "discountedPrice": 66,
103 * "formattedPrice": "$100.00",
104 * "formattedDiscountedPrice": "$66.00",
105 * "inventoryItemId": "65ef525c-...-43c67",
106 * "discount": {
107 * "type": "AMOUNT",
108 * "value": 5
109 * },
110 * "trackInventory": false,
111 * "inStock": true,
112 * "additionalInfoSections": [ ],
113 * "productOptions": {
114 * "Color": {
115 * "optionType": "drop_down",
116 * "name": "Color",
117 * "choices": [
118 * {
119 * "value": "Black",
120 * "description": "Black",
121 * "inStock": true,
122 * "visible": true,
123 * "mainMedia": "wix:image://v1/1c111c1...1111cc111~mv2.jpg/1c411c1...1111cc111~mv2.jpg#originWidth=50&originHeight=50",
124 * "mediaItems": [
125 * {
126 * "id" : 0d26de75...5379.jpg,
127 * "src" : wix:image://v1/0d26de75...5379.jpg/file.jpg#originWidth=1000&originHeight=1776,
128 * "description" : ,
129 * "title" : ,
130 * "type" : Image
131 * }
132 * ]
133 * },
134 * {
135 * "value": "Blue",
136 * "description": "Blue",
137 * "inStock": true,
138 * "visible": true,
139 * "mainMedia": null,
140 * "mediaItems": [ ]
141 * }
142 * ]
143 * }
144 * },
145 * "productPageUrl": "/product-page/pants",
146 * "customTextFields": [ ],
147 * "manageVariants": true,
148 * "productType": "physical",
149 * "slug": "pants",
150 * "collections": [e22737a6-...-3fcfd5a"]
151 * "variants": [
152 * {
153 * "_id": "00000000-...-9aed0d5c3297",
154 * "choices": {
155 * "Color": "Black"
156 * },
157 * "variant": {
158 * "currency": "USD",
159 * "price": 70,
160 * "discountedPrice": 66,
161 * "formattedPrice": "$70.00",
162 * "formattedDiscountedPrice": "$66.00",
163 * "weight": 1,
164 * "sku": "",
165 * "visible": true
166 * }
167 * },
168 * {
169 * "_id": "00000000-...-9aed0d5c3297",
170 * "choices": {
171 * "Color": "Blue"
172 * },
173 * "variant": {
174 * "currency": "USD",
175 * "price": 70,
176 * "discountedPrice": 66,
177 * "formattedPrice": "$70.00",
178 * "formattedDiscountedPrice": "$66.00",
179 * "weight": 1,
180 * "sku": "",
181 * "visible": true
182 * }
183 * }
184 * ]
185 * }
186 * }
187 */