I don't have any explicit examples at the moment, but it would ultimately mean generating the JSON data for the property editor manually which is based on the block editor data format.
Best bet really is to take a look at the stored value for a variants property and look to replicate it in JSON form yourself.
Key thing really is the config element on the layout items which holds the attribute configuration where the key value pares are the aliases of Product Attribute combinations. It also holds a flag to state whether a variant is the default variant option.
PS You shouldn't need to explicitly set the storeId value as this will get injected in when you save the document. This is to ensure that the variants property editor and it's value converter always know it's store context.
contentUdi refers to the udi property of the elements in the contentData section, which these can effectively be new Guids. So whatever Guid you set in the contentData section for a variants data, you just need to ensure the entry in the layout section links to it.
Create product variants from code
Hi, I have such a question. Has anyone tried to create variants for the product through the code, and if so, can I have examples?
I don't have any explicit examples at the moment, but it would ultimately mean generating the JSON data for the property editor manually which is based on the block editor data format.
Best bet really is to take a look at the stored value for a variants property and look to replicate it in JSON form yourself.
Matt
This may help you to create the variant JSON data programmatically: https://our.umbraco.com/documentation/getting-started/backoffice/property-editors/built-in-property-editors/Block-List-Editor/#creating-blocklist-programmatically
Not sure if the the variants property editor has exact same JSON structure though.
@Matt maybe you can post the base JSON format used in the variants property editor?
/Bjarne
Sure, our data structure is:
Key thing really is the config element on the layout items which holds the attribute configuration where the key value pares are the aliases of Product Attribute combinations. It also holds a flag to state whether a variant is the default variant option.
PS You shouldn't need to explicitly set the
storeId
value as this will get injected in when you save the document. This is to ensure that the variants property editor and it's value converter always know it's store context.Thanks for the answer! Then another question, where should I get contentUdi, or how to create it?
Hi Andrew,
contentUdi
refers to theudi
property of the elements in thecontentData
section, which these can effectively be new Guids. So whatever Guid you set in thecontentData
section for a variants data, you just need to ensure the entry in thelayout
section links to it.Matt
Thank you so much!
You're more than welcome.
Hope you manage to get it all working, but feel free to ask if you hit any other walls.
Matt
Hi Andrew
Regarding the element types you could use ContentTypeService, e.g.
and generate the
contentTypeKey
:Regarding
contentUdi
andudi
it can be generated like this:Another way to generate the UDI format:
You can read more about the format here: https://our.umbraco.com/documentation/reference/querying/Udi
Thanks for the answer!
is working on a reply...