Copied to clipboard

Flag this post as spam?

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


  • Alessandro 31 posts 152 karma points
    Mar 30, 2022 @ 16:39
    Alessandro
    0

    Examine custom field with Vendr Price is Null

    Hello Matt, I'm trying to sort my product list by price, but I have the following problem.

    What I'd like to do is:

    criteria.OrderBy(new SortableField("decimalPrice", SortType.Float));
    

    And I've added a custom field to Examine Index, like this:

        private void IndexerComponent_TransformingIndexValues(object sender, IndexingItemEventArgs e)
        {
            if (int.TryParse(e.ValueSet.Id, out var nodeId))
            {
                using (var umbracoContext = umbracoContextFactory.EnsureUmbracoContext())
                {
                    IPublishedContent content = (IPublishedContent)umbracoContext.UmbracoContext.Content.GetById(nodeId);
    
                    if(content != null)
                    { 
                        switch (e.ValueSet.ItemType)
                        {                   
                            case "corso":
                            {
                                Corso corso = (Corso)content;
                                if(corso != null)
                                {
                                    e.ValueSet.Set("parent", corso.Parent.Name.ToLower());
                                    e.ValueSet.Set("decimalPrice", corso.Price.First().Value);
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }
    

    And the component is registered like this:

     [RuntimeLevel(MinLevel = RuntimeLevel.Boot)]
    public class BoomComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            // Append components
            composition.Components().Append<ExamineComponent>();
        }
    }
    

    Unfortunately, when I rebuild the index the corso.Price property (which is the Vendr Price) appears to be null.

    What could be wrong?

    Thanks in advance, Alessandro

  • Alessandro 31 posts 152 karma points
    Mar 30, 2022 @ 17:08
    Alessandro
    0

    Uhm... i managed to get by somehow but not very clear why...

    At first, I tried to replace this

    corso.Price.First().Value
    

    With this

    corso.CalculatePrice().WithoutTax
    

    but still got the same error, then I tried to unfold the method

    var snap = VendrApi.Instance.GetProduct(new Guid("34972299-53ec-4c27-9abf-017ed5702137"), corso.Key.ToString(), "it-IT");
    if(snap.Prices.Count() > 0)
    {
        e.ValueSet.Set("decimalPrice", snap.Prices.First().Value);
    }
    else
    {
        e.ValueSet.Set("decimalPrice", 0);
    }
    

    And this appears to be working. So possibly there is something between the store GUID and the locale which is not set when rebuilding index?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Mar 31, 2022 @ 07:46
    Matt Brailsford
    0

    Hmmm, I'm not entirely sure why it would be null if it has a value. The product snapshot API just reads the same property and reviewing the price property value converter, it doesn't rely on anything that could cause an error so I'm not sure why it should be null 🤔

  • Alessandro 31 posts 152 karma points
    Mar 31, 2022 @ 08:12
    Alessandro
    0

    Hi Mat, actually this

    corso.CalculatePrice().WithoutTax
    

    was working. I thought I had the same error but actually it was from an item without a price set.

    This

    corso.Price.First().Value
    

    instead seemed to fail also with items with a price set, but I'm not so sure anymore... so please ignore this ticket.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft