Copied to clipboard

Flag this post as spam?

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


  • Kyle Eck 130 posts 280 karma points
    Dec 17, 2021 @ 02:06
    Kyle Eck
    0

    Create Complex Variants from code

    Matt,

    I am looking to create complex product variants from code using CMS Import. So far I have the CMS Import part down pat but I was curious how to go about mapping a Preset to attributes.

    For example, if I have a Glove Size Preset, I would want to put the attributes I programmatically create under the Glove Size Preset.

    How does this structure determine the preset it applies too?

    {
    "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"
    

    }

    I understand the structure and how the Guids relate etc. but am concerned how to relate the attributes I create relate to a Preset if that makes sense?

    I know this is sort of unexplored terrain at this point and meshing two systems can be tedious but I have successfully mapped a CSV price value to a Vendr.Price data type successfully, so progress is progress.

    Thanks for your continued effort with all of the recent questions.

    • Kyle
  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Dec 17, 2021 @ 09:23
    Matt Brailsford
    0

    Hi Kyle,

    Really nice work on getting this CMSImport stuff working. I'd be REAALY interested to see this when it's all done.

    Regarding presets, there really isn't any linkage between the variants and the preset to track, rather presets are just pre-defined filters used by the UI when you create your variant so it will just limit the number of attributes you can pick from. The only linkage a variant has is with the attributes themselves so that's really all you need to worry about.

    Looking forward to see how this goes

    Matt

  • Kyle Eck 130 posts 280 karma points
    Dec 17, 2021 @ 11:21
    Kyle Eck
    0

    Matt, so if I’m understanding correctly as long as I create my stuff with the correct attribute aliases and such everything should line up?

    I have been talking with Richard so I think between the experience you two hold I can get this working…

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Dec 17, 2021 @ 11:24
    Matt Brailsford
    0

    Hey Kyle,

    That's correct yes. So long as the aliases match, you should be good 👍

    Glad we can work together to get you there 😁

    Matt

  • Kyle Eck 130 posts 280 karma points
    Dec 17, 2021 @ 11:28
    Kyle Eck
    0

    Awesome,

    I’ll get started on it and let you know if I come across any hiccups.

    I am genuinely surprised no one has traveled down this road prior as it seems like something that could be really useful.

    Thanks Matt, I will keep in touch

  • Kyle Eck 130 posts 280 karma points
    Dec 17, 2021 @ 11:30
    Kyle Eck
    0

    Matt,

    One more thing, if you want the field provider code for a Vendr.Price let me know. It’s fairly generic as I am using a CSV file right now

    I’d be happy to provide it.

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Dec 17, 2021 @ 11:50
    Matt Brailsford
    0

    Definitely 👍

  • Kyle Eck 130 posts 280 karma points
    Dec 17, 2021 @ 14:06
    Kyle Eck
    1

    Matt,

    I posted this also over in the CMS Import - Using the API support forum, but here you go.

    Like I said it is fairly generic.

    1. Put the .cs file in your controllers folder of your solution, obviously make sure it builds and compiles
    2. Start an Import in CMS Import
    3. The Parse method should get hit by a breakpoint when you put one in the method

    You should also be careful to make sure the class inherits from an IFieldProvider so that CMS Import knows what its working with. Hope that makes sense

    Here is the Attribute Declaration for the entirety of the class (this tells CMS Import to only fire this when we come across a Price cell from a CSV File) This needs to be the datatype your putting values into, hence Vendr.Price

        [FieldProvider(PropertyEditorAlias = "Vendr.Price")]
    

    Here is the Dependiency Injection of the Umbraco Content Service if needed

        private readonly IContentService _contentService;
        public VendrCurrencyFieldProvider(IContentService contentService)
        {
            _contentService = contentService;
        }
    

    Here is the Parse Method (Responsible for data processing)

    public object Parse(object value, ImportPropertyInfo property, FieldProviderOptions fieldProviderOptions)
        {
    
            //create new decimal to hold value
            var price = new Decimal();
    
            //if value is blank in your source file (I used CSV) then compensate for that and place a value you
            //would know into the price field so you can either manually change it or account for it in some way
            if (value != null && value.AsString() != "")
            {
                price = Decimal.Parse(value.AsString());
            }
            else
            {
                price = Decimal.Parse("1234.56");
            }
    
            //create the data structure you need to send the data in
            //in the case of Vendr.Price you need { currencyId guid (found in back office) , price value from source file }
            var data = new Dictionary<string, decimal> {
                { "05c1c00a-fd2d-41f9-b5af-017d7c739f60", price }
            };
    
            //serialize the data and return the value - done
            return JsonConvert.SerializeObject(data);
        }
    

    Let me know if you need anything else

  • Kyle Eck 130 posts 280 karma points
    Dec 23, 2021 @ 02:11
    Kyle Eck
    0

    Matt,

    Following up here...I have the raw data formatted and being recieved by CMS Import all the way until it tries to map my Variant Editor block style code to the field...

    I wanted to check if this was accurate...

    I have a field in umbraco called Product Variants, which is of the Data Type Vendr.VariantsEditor.

    Now the way you tell CMS Import to look for a specific field and prepare the data is to use something like this:

    [FieldProvider(PropertyEditorAlias = "Vendr.VariantsEditor")]
    

    I have price working in a similar fashion, just cant get the VariantEditor to work as such.

    Vendr.VariantsEditor is in fact the correct nomenclature for that field correct?
    

    Thanks

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Dec 23, 2021 @ 10:03
    Matt Brailsford
    0

    Hmm, the alias is correct, but I'm not sure what the error there is, that might be one for Richard.

    Just on another point, the variants property on your doctype need to be named variants so you might want to change that, but I don't believe this is your problem here.

    Matt

  • Kyle Eck 130 posts 280 karma points
    Dec 23, 2021 @ 11:21
    Kyle Eck
    0

    What does changing my alias from “Product Variants” to “Variants” actually do in terms of Vendr?

    I’ll change my property alias, to conform, just curious what it does when having that alias?

    Is that what you look at to display the variants in the back office?

    Thanks.

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

    Hi Kyle,

    It's just that's the property alias we assume for the variants editor as we will have to look up the value at various points.

    You can review the docs here for a list of properties that can be defined on an Umbraco node and what Vendr expects their alias to be https://vendr.net/docs/core/2.0.0/umbraco-v9/key-concepts/umbraco-properties/

    Matt

    Lol, actually just reliased variants isn't on that list 🤦‍♂️ Something for me to fix in the new year 😁

  • Kyle Eck 130 posts 280 karma points
    Dec 23, 2021 @ 11:27
    Kyle Eck
    1

    Matt,

    Lol Gave me a good laugh as that’s what I used to see if I needed to name it something specific. I understand why it needs to be that specific, just wasn’t aware to make it that Alias.

    It’s all good

Please Sign in or register to post replies

Write your reply to:

Draft