Search.../

addProductMedia( )

Adds media items by ID to a product.

Description

The addProductMedia() function returns a Promise that resolves when the adding of the media (images or videos) to a product has started.

Limitation The URL is not validated and no event is triggered to indicate if the media was added successfully.

Syntax

function addProductMedia(productId: string, media: Array<Media>): Promise<void>

addProductMedia Parameters

NAME
TYPE
DESCRIPTION
productId
string

Product ID.

media
Array<Media>

Sources of media items already uploaded to the Wix site.

Returns

Fulfilled - When the media are added to the product. Rejected - Error message.

Return Type:

Promise<void>

Was this helpful?

Add media items to a product

Copy Code
1/*******************************
2 * Backend code - products.jsw *
3 *******************************/
4
5import wixStoresBackend from 'wix-stores-backend';
6
7export function addProductMedia(productId, mediaData) {
8 return wixStoresBackend.addProductMedia(productId, mediaData);
9}
10
11/*************
12 * Page code *
13 *************/
14import { addProductMedia } from 'backend/myModule';
15
16
17// ...
18
19const productId = ...; // get product ID
20const src = "wix:image://v1/1a11a1_..._1a~mv2.jpg/1a11a1_..._1a~mv2.jpg";
21const choice = "Color";
22const option = "Blue";
23
24if (choice !== "" && option !== "") {
25 const mediaData = [{ // add media item to a choice
26 src,
27 "choice": {
28 choice,
29 option
30 }
31 }]
32
33} else {
34 const mediaData = [{ // add media item to the product
35 src
36 }]
37}
38
39addProductMedia(productId, mediaData)