Hi folks,
no matter what I do, I can't make `updateVariantData` to work.
No matter what I pass in the variantInfo, it always fails.
This API looks weird to me anyway. How would those variants will be identified since we're not passing any variant id?
export async function get_updateVariants(request) {
const variantInfo = [{
visibility: false,
"choices": {
test: "1"
}
}]
let options = {};
const productId = '0109b361-d1fb-4e0a-ad87-9ff76dc9d932'
try {
const result = await updateVariantData(productId, variantInfo)
return ok(options);
} catch (error) {
options.body = {
"error": error,
};
return serverError(options);
}
}
Have you figured this out? I agree with you that the docs seem to be off, the choices-array is not part of the list of members of VariantInfo type, but it is used in the corresponding codeSample is something called a VariantData.... I think the assumption that your are already on the productPage does not make alot of sense, since you might want to update the variants BEFORE displaying it. I'm also using wixdata to get the product.
How are we supposed to know what exactly you are doing when you don't show the code that you are using for it?
Choice and Choices are in the Wix Stores Backend themselves.
https://www.wix.com/corvid/reference/wix-stores-backend.html#Choice
Choice An object representing the choice for a product variant. Syntax type Choice = { choices: Object } choices Object Product options to use when creating or updating the product. The object contains key:value pairs where the key is the option name and the value is the chosen option value.
https://www.wix.com/corvid/reference/wix-stores-backend.html#Choices
Choices An object representing the choice for a product variant. Syntax type Choices = { option: string choice: string option string Option name. choice string Choice name.
If you are going by that then the product id will be obtained from the product page and the getProduct function itself in the code on your product page.
https://www.wix.com/corvid/reference/$w.ProductPage.html#getProduct
getProduct( )
Gets the all the information associated with the current product.
Note
The Manage Variants field for the product must be set to true to update a variant's data.
Examples
Update the data of a variant.
/**************** * backend code * ****************/ import wixStoresBackend from 'wix-stores-backend'; export function updateVariantDataBackend(productId, variantData) { return wixStoresBackend.updateVariantData(productId, variantData); }
/******************** * client-side code * ********************/ import {updateVariantDataBackend} from 'backend/myModule'; // ... $w('#myProductPage').getProduct() .then( (product) => { let productId = product._id; let productManageVariants = product.manageVariants; // check that variants can be added and updated // for this product if (productManageVariants) { const variantChoice = "Color"; const variantChoiceValue = "Blue"; const weight = 10; const price = 15; const variantData = [{ weight, price, "choices": { variantChoice: variantChoiceValue } }]; updateVariantDataBackend(productId, variantData) .then( () => { // product variant data have been updated } ) .catch( (error) => { // there was an error updating the product variant } ); } } )
if the updateVariantData always fails, can you maybe show the code of the function?