Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AndrewMorgun 10 posts 87 karma points
    Apr 14, 2021 @ 10:26
    AndrewMorgun
    0

    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?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Apr 14, 2021 @ 12:27
    Matt Brailsford
    0

    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

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Apr 14, 2021 @ 12:38
    Bjarne Fyrstenborg
    0

    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

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Apr 14, 2021 @ 13:14
    Matt Brailsford
    0

    Sure, our data structure is:

    {
        "layout":{
            "Vendr.VariantsEditor":[
                {
                    "contentUdi":"umb://element/0da576e5fc7445bd9d789c5ee9fe9c54",
                    "config": {
                        "attributes": {
                            "size": "lg",
                            "color": "blue",
                            "fit": "mens"
                        },
                        "isDefault": true
                    }
                },
                {
                    "contentUdi":"umb://element/c6e23e8137d24b409bb5ef41bdb705b9",
                    "config": {
                        "attributes": {
                            "size": "xl",
                            "color": "green",
                            "fit": "mens"
                        },
                        "isDefault": false
                    }
                }
            ]
        },
        "contentData":[
            {
                "contentTypeKey":"aca1158a-bad0-49fc-af6e-365c40683a92",
                "udi":"umb://element/0da576e5fc7445bd9d789c5ee9fe9c54",
                "sku":"SKU-001",
                "description":"Variant 1 description"
            },
            {
                "contentTypeKey":"aca1158a-bad0-49fc-af6e-365c40683a92",
                "udi":"umb://element/c6e23e8137d24b409bb5ef41bdb705b9",
                "sku":"SKU-002",
                "description":"Variant 2 description"
            }
        ],
        "storeId": "7b2e6800-2e1e-4c51-89cc-cf5ded4e8566"
    }
    

    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.

  • AndrewMorgun 10 posts 87 karma points
    Apr 14, 2021 @ 14:57
    AndrewMorgun
    0

    Thanks for the answer! Then another question, where should I get contentUdi, or how to create it?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Apr 14, 2021 @ 15:03
    Matt Brailsford
    0

    Hi Andrew,

    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.

    Matt

  • AndrewMorgun 10 posts 87 karma points
    Apr 14, 2021 @ 15:19
    AndrewMorgun
    0

    Thank you so much!

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Apr 14, 2021 @ 15:23
    Matt Brailsford
    0

    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

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Apr 14, 2021 @ 18:01
    Bjarne Fyrstenborg
    0

    Hi Andrew

    Regarding the element types you could use ContentTypeService, e.g.

    IContentTypeService contentTypeService = Services.ContentTypeService;
    var contentTypes = contentTypeService.GetAll();
    var multiVariantType = contentTypes.Where(n => n.Alias == "productMultiVariant").FirstOrDefault();
    

    and generate the contentTypeKey:

    { "contentTypeKey", multiVariantType.Key.ToString() }
    

    Regarding contentUdi and udiit can be generated like this:

    GuidUdi contentUdi = new GuidUdi("element", System.Guid.NewGuid());
    ...
    
    { "udi", contentUdi.ToString() }
    

    Another way to generate the UDI format:

    GuidUdi guidUdi = GuidUdi.Create(Umbraco.Core.Constants.UdiEntityType.Element, System.Guid.NewGuid());
    Udi udi = Udi.Create(Umbraco.Core.Constants.UdiEntityType.Element, System.Guid.NewGuid());
    

    You can read more about the format here: https://our.umbraco.com/documentation/reference/querying/Udi

  • AndrewMorgun 10 posts 87 karma points
    Apr 15, 2021 @ 06:52
    AndrewMorgun
    0

    Thanks for the answer!

Please Sign in or register to post replies

Write your reply to:

Draft