Copied to clipboard

Flag this post as spam?

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


  • Kevin 35 posts 159 karma points
    Oct 19, 2016 @ 15:15
    Kevin
    0

    Get ProductOption ContentType Umbraco data in code

    In Umbraco I have created a Document Type called "Shared Option Image" which has an image picker.

    In Merchello I have set "Product Option Content Types" to include "Shared Option Image".

    I have created a shared option e.g. colour (just an example), and assigned images yellow.jpg, green.jpg and blue.jpg.

    I have created a product, assigned it some shared options and can see the variants.

    In code, how do I access the "Shared Option Image" content type data of the option for a product. I have:

    var merchello = new MerchelloHelper();
    var productQuery = merchello.Query.Product;
    var product = productQuery.GetByKey(productKey);
    var productOption = product.ProductOptions.FirstOrDefault();
    

    Now I'm stuck. Any help appreciated.

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Oct 19, 2016 @ 16:22
    Rusty Swayne
    0

    Hey Kevin,

    You're getting hung up on an API that I'm slowly trying to obsolete - productQuery is returning ProductDisplay and you want IProductContent.

    Try:

    var product = productQuery.TypedProductContent(productKey);
    
    var productOption = product.ProductOptions.FirstOrDefault();
    
    foreach(var choice in productOption.Choices)
    {
            var image = choice.GetPropertyValue("image");
    }
    

    FYI - We're working on cleaning these up and making that portion of the API much cleaner.

  • Kevin 35 posts 159 karma points
    Oct 19, 2016 @ 16:44
    Kevin
    0

    Thanks Rusty, however your example may not be correct?

    This line won't compile

    var image = choice.GetPropertyValue("image");
    

    'ProductAttributeDisplay' does not contain a definition for 'GetPropertyValue'

    Your way is better than mine if we can get it working - I just got this far to get the image ID.

      var product = product.AsProductContent();
      var option = product.ProductOptions.FirstOrDefault();
        foreach (var choice in option.Choices)
        {
            foreach (var prop in choice.Properties)
            {
                if (prop.PropertyTypeAlias.Equals("ImageFieldName"))
                {
                    var imageId = prop.Value;
    
                    // get the image URL
                    // TODO:
                }
            }
    }
    

    As a side question ,which part of my original example are you trying to make obsolete? The whole MerchelloHelper Query or just the GetByKey method?

    Thanks again.

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Oct 19, 2016 @ 17:00
    Rusty Swayne
    100

    I apologize, your correct - the ProductOption collection is another place where we could not make changes immediately due to too many breaking changes.

       // use the new "Options" collection
       var option = product.Options.FirstOrDefault();
    

    Also, we're going to hopefully move the .AsProductContent() extension internal in V3 as it can circumvent the caching ... so try to stick with the Query.Product.TypeProductContent(productKey)

  • Kevin 35 posts 159 karma points
    Oct 19, 2016 @ 17:24
    Kevin
    0

    That has sorted it - thank you.

  • David Godfrey 14 posts 83 karma points
    Jul 26, 2017 @ 19:00
    David Godfrey
    0

    Hi All,

    I am working on something very similar, I have added images with some JS and hidden the dropdown lists to allow the user to click on an image to select size and colour.

    This all works perfectly however I can only get it to work on 1 product at a time. My site is based on 3 products and they all have similar product choices size and colour.

    If i build the project the first product i click on after the build (locally) will show the image selection but the other 2 products wont.

    I need all 3 to work but cant understand why only 1 will work at a time.

    Any ideas?

    Thanks

    David

Please Sign in or register to post replies

Write your reply to:

Draft