Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    Feb 04, 2019 @ 10:44
    Gordon Saxby
    0

    Checkboxes on Variants

    Umbraco v7.4.2, TC v3.1.1

    I have added 2 checkboxes (True/False) to my Variant DocType, but when I try to access the value, it always returns false:

    variant.GetPropertyValue<bool>("currentlyUnavailable")
    

    Where "variant" is of type VariantPublishedContent.

    I have a number of Textstring fields and they work, I also have a dropdown and that works - but the checkboxes don't. Am I doing something wrong?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 04, 2019 @ 15:43
    Matt Brailsford
    0

    What happens if you just call GetPropertyValue without the generic type?

  • Gordon Saxby 1461 posts 1883 karma points
    Feb 04, 2019 @ 15:51
    Gordon Saxby
    0

    The same result - always false (actually 'False')

    variant.GetPropertyValue("currentlyUnavailable")
    
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 04, 2019 @ 16:16
    Matt Brailsford
    0

    Are you able to look into the cmsPropertyData DB table and try and find the stored value? It'll be interesting to see if it's a problem saving it, or a problem retreiving it.

  • Gordon Saxby 1461 posts 1883 karma points
    Feb 04, 2019 @ 16:23
    Gordon Saxby
    0

    In the Variant JSON, it is stored as

    "currentlyUnavailable": "1",
    
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 04, 2019 @ 16:24
    Matt Brailsford
    0

    Ok, so it is being stored ok. And does it all work ok in the back office? it remembers the value etc when you move to and from a product?

  • Gordon Saxby 1461 posts 1883 karma points
    Feb 04, 2019 @ 16:30
    Gordon Saxby
    0

    Yes, the back office works OK.

    I have managed to get the data value by overriding the Variant class (ModelsBuilder):

        [ImplementPropertyType("currentlyUnavailable")]
        public bool CurrentlyUnavailable
        {
            get
            {
                var rawVal = Properties.FirstOrDefault(t => t.PropertyTypeAlias == "currentlyUnavailable");
                if (rawVal == null) return false;
    
                return (rawVal.DataValue.ToString() == "1");
            }
        }
    

    Which is OK, but it just means I have to create a Variant object just to get the value of the checkboxes.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 04, 2019 @ 16:33
    Matt Brailsford
    0

    Ahh, ok, so this is via ModelsBuilder. How are you actually getting your variant? I'm intregued how you are actually using it with ModelsBuilder 🤔

  • Gordon Saxby 1461 posts 1883 karma points
    Feb 04, 2019 @ 16:39
    Gordon Saxby
    0

    I get all variants for the product using

    var variants = TC.GetVariants(storeId, productPage).ToList();
    

    then loop through using

                        foreach (var variant in variants)
    

    Currently, I need to create a Variant

                            var  varRecord = new Variant(variant);
    

    (although I could have the previously mentioned code in-line, I suppose)

    So, the first version works but the second doesn't

    var unavail = varRecord.CurrentlyUnavailable;
    var unavail = variant.GetPropertyValue<bool>("currentlyUnavailable");
    

    In the end, what I have now is working and looks cleaner, but TC does appear to have a problem getting the value for a checkbox.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 04, 2019 @ 16:51
    Matt Brailsford
    0

    Strange. I'll try and replicate it this evening and see what I can find.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 04, 2019 @ 18:10
    Matt Brailsford
    0

    Typical. I've just tested this on my local demo install (Umbraco 7.12.4 though) and this worked as it should.

    Add checkbox to variants doctype

    enter image description here

    Add property to variants data type as extra info

    enter image description here

    Set values on variants

    enter image description here

    Loop through variants in template

    enter image description here

    Output values of onSale property to screen

    enter image description here

  • Gordon Saxby 1461 posts 1883 karma points
    Feb 05, 2019 @ 09:11
    Gordon Saxby
    0

    Yes, typical indeed! At least I have a method for making it work, so I'll carry on with that.

    Thanks for your help.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Feb 05, 2019 @ 10:24
    Matt Brailsford
    0

    No problem. Sorry I couldn't track it down for you, but glad you at least have a workaround.

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Feb 06, 2019 @ 18:35
    Paul Wright (suedeapple)
    0

    I think this might be something to do with the version of ModelsBuilder that's being used, and how the core ValueConverters have changed.

    Your override should work fine on this occasion, and is something I've done myself on legacy umbraco sites.

    v7.4.2 is quite an old version of Umbraco :-)

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Feb 06, 2019 @ 18:47
    Paul Wright (suedeapple)
    0

    Further more... this might be insightful

    https://issues.umbraco.org/issue/U4-9925

    Also, Looking at the docs, V7.4.2 is where the True/False with a default value option was introduced - so likely to have been buggy :-)

    https://our.umbraco.com/documentation/getting-started/backoffice/property-editors/built-in-property-editors/true-false

Please Sign in or register to post replies

Write your reply to:

Draft