Hello, i did see one subject only on that : BUG? wix-stores-backend => addProductMedia() | Velo by Wix
But there seems a bug when trying to add media to a product. You can’t using “mainMedia” or “mediaItems” as a variable in your product creation, and i don’t see a way to put media on created products…
While using the API exemple here : addProductMedia - Velo API Reference - Wix.com , there is an error triggering in the console log :
I saw @yisrael-wix reacting on the post mentionned up there, and there seems like the API reference is the same…
Any thought on that ?
here is the code !
import {
    createProduct,
    addProductsToCollection,
    addProductMedia
} from 'backend/products';
$w.onReady(function () {
    /*  $w("#addProductButton").onClick(async () => {
      */
})
let galleryPictures = [];
export async function galleryUploadBtn_change(event) {
    let imageToGallery = {};
    let file = $w('#galleryUploadBtn').value;
    if (file.length > 0) {
        const response = await $w('#galleryUploadBtn').startUpload($w('#galleryUploadBtn').buttonLabel = 'Chargement ...');
        if (response) {
            $w('#galleryUploadBtn').buttonLabel = 'Images';
            imageToGallery.src = response.url;
            imageToGallery.type = 'image';
            console.log('gallery', galleryPictures);
            galleryPictures.push(imageToGallery);
            imageToGallery = {};
        }
    }
    galleryDisplayer();
}
function galleryDisplayer() {
    $w('#innGallery').items = galleryPictures;
    $w('#Gallery').expand();
}
export async function videoUploadBtn_change(event) {
    let videoToGallery = {};
    let file = $w('#videoUploadBtn').value;
    if (file.length > 0) {
        const response = await $w('#videoUploadBtn').startUpload($w('#videoUploadBtn').buttonLabel = 'Chargement ...');
        if (response) {
            $w('#videoUploadBtn').buttonLabel = 'Videos';
            videoToGallery.src = response.url;
            videoToGallery.type = 'video';
            console.log('gallery', galleryPictures);
            galleryPictures.push(videoToGallery);
            videoToGallery = {};
        }
    }
    galleryDisplayer();
}
export function clear_click(event) {
    galleryPictures = [];
    galleryDisplayer();
}
    
export async function addProductButton_click(event) {
    console.log("clicked")
    const product = {
        "name": $w("#input1").value,
        "description": $w("#textBox1").value,
        "price": $w("#input2").value,
       // "mainMedia" : galleryPictures[0].src,
      //  "mediaItems": galleryPictures,
        "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 = '95a1eef4-7a2b-4674-ec21-a2361683269c'
        addProductsToCollection(collectionId, newProducts);
        // The product was assigned to a collection.
        // Now let's add media.
         const option =  "Color"
         const choice =  "Black"
         const src =  galleryPictures[0].src
        const mediaData = [{'src':src}]
        console.log(mediaData)
 /*             // 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
    }
