Copied to clipboard

Flag this post as spam?

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


  • Tomasz Kowalski 135 posts 445 karma points
    May 10, 2021 @ 09:41
    Tomasz Kowalski
    0

    Convert PricePropertyValue to IEnumerable<ProductPrice> in a custom ProductAdapter

    Hi,

    All my products has the same price, more precise one of two: regular or discount. Price depends on a True/False property on the current member.

    There is no price property in the product document type. It is moved to the Home document type.

    In my custom product adapter, I get a product snapshot and update it with prices from Home node (regular or discount).

    I have had problems with converting price from PricePropertyValue to IEnumerable

    I'm injecting CurrencyService to get all available currencies, and using lambda to produce list of prices, but I'm not sure if it is a good solution:

    public override IProductSnapshot GetProductSnapshot(string productReference, string productVariantReference, string languageIsoCode)
        {
            var snapshot = (UmbracoProductSnapshot)base.GetProductSnapshot(productReference, productVariantReference, languageIsoCode);
            if (snapshot != null)
            {
                var homeNode = _umbracoContextAccessor.UmbracoContext.Content.GetAtRoot().Where(x => x.ContentType.Alias.ToLower() == "home").OfType<Home>().FirstOrDefault();
                if (homeNode != null)
                {
                    var nodePrice = homeNode.StandardPrice;
                    var loggedMember = _umbracoContextAccessor.UmbracoContext.HttpContext.User.Identity;
                    if (loggedMember != null)
                    {
                        var member = memberService.GetByUsername(loggedMember.Name);
                        if (member != null)
                        {
                            var hasDiscountPrice = member.GetValue<bool>("reducedPrice");
                            if (hasDiscountPrice)
                            {
                                nodePrice = homeNode.DiscountPrice;
                            }
                        }
                    }
                    var currencies = currencyService.GetCurrencies(homeNode.Store.Id);
                    var newPrices = currencies.Select(x => nodePrice.GetPriceFor(x.Id));
    
                    return new CustomProductDecorator(snapshot, newPrices);
                }
            }
    
            return null;
        }
    

    There should be better way to do this conversion?

    Kind regards

    Tomasz

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    May 10, 2021 @ 10:00
    Matt Brailsford
    0

    Hi Tomasz,

    Hmm, yea, it looks like that is currently the only way to do it. Looking at this, I could probably just make the PricePropertyValue itself IEnumerable<ProductPrice> which should do the trick.

    I'll raise a feature request on the issue tracker.

    Matt

  • Tomasz Kowalski 135 posts 445 karma points
    May 10, 2021 @ 10:07
    Tomasz Kowalski
    0

    Hi, Matt

    Thanks for a quick reply, as always :)

    Anyway, it's good to know, that I've done it right :)

    Tomasz

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    May 10, 2021 @ 10:13
    Matt Brailsford
    100

    I've just implemented this locally and will be in an upcoming 1.8.1 release.

    Until then, yea, what you have is probably the best approach. 👍

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft