Copied to clipboard

Flag this post as spam?

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


  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Sep 08, 2020 @ 11:01
    Søren Kottal
    0

    Can't add a Product to an Order where there is no price for the Order currency.

    Trying to set up a POC for a Vendr shop with an external product catalog.

    The ProductSnapshot looks like this

    public class Product : IProductSnapshot
    {
        public int Id { get; set; }
        public string ProductReference => Id.ToString();
        public string Sku => Id.ToString();
        public string UrlSegment => string.Format("{0}-{1}", Name, Sku).ToSafeAlias();
        public Guid StoreId => new Guid("c7624842-d284-464b-aee0-b99e42c1993b"); // hardcoded for speed :)
        public IEnumerable<ProductPrice> Prices { get; set; }
        public IDictionary<string, string> Properties
        {
            get
            {
                var dict = new Dictionary<string, string>();
                dict.Add("Category", Category.Name);
                dict.Add("Description", Description);
                return dict;
            }
        }
        public Category Category { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string ImageUrl { get; set; }
    
        public Guid? TaxClassId => new Guid("6d12d936-f58a-44ef-a4ee-3b7c60b3d22b"); // hardcoded for speed :)
    
        public bool IsGiftCard => false;
    }
    

    And is created from a CSV like this

                int.TryParse(x[0], out int id);
                decimal.TryParse(x[6].Replace(".", "").Replace(" DKK", ""), out decimal price);
    
                var category = categories.FirstOrDefault(c => c.Name.InvariantEquals(x[1]));
    
                var prices = new List<ProductPrice>();
                prices.Add(new ProductPrice(price, new Guid("0beb35b4-f907-49c4-b114-8aecd7f7925e")));
    
                return new Product()
                {
                    Id = id,
                    Category = category,
                    Name = x[2],
                    Prices = prices,
                    ImageUrl = x[8]
                };
    

    And the product adapter looks like this

        public IProductSnapshot GetProductSnapshot(string productReference, string languageIsoCode)
        {
            return ProductRepo.GetProduct(productReference);
        }
    
        public string GetProductReference(Guid storeId, string sku)
        {
            // SKU and ProductReference is the same in the POC, so just return SKU
            return sku;
        }
    

    When I add the product to the cart, I do it like this:

                using (var uow = _unitOfWorkProvider.Create())
                {
                    var store = CurrentPage.GetStore();
                    var product = ProductRepo.GetProduct(postModel.ProductReference);
    
                    var order = _sessionManager.GetOrCreateCurrentOrder(store.Id)
                        .AsWritable(uow);
    
                    order = order.AddProduct(product, postModel.Quantity);
    
                    _orderService.SaveOrder(order);
    
                    uow.Complete();
                }
    

    It then breaks and gives the message Can't add a Product to an Order where there is no price for the Order currency..

    I have verified that the currency on the product price is the same as the currency on the order.

    Am I missing something on the ProductSnapshot?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Sep 08, 2020 @ 11:12
    Matt Brailsford
    100

    Hey Soren,

    The product adapter looks correct to me.

    That error is coming from a validation rule which is pretty simple in terms of what it checks

    var productPrices = _productService.Value.GetProduct(evt.ProductReference)?.Prices;
    var currencyPrice = productPrices?.FirstOrDefault(x => x.CurrencyId == evt.Order.CurrencyId);
    if (currencyPrice == null)
        evt.Fail($"Can't add a Product to an Order where there is no price for the Order currency.");
    

    So I'd initially say to check whether your prices are coming back with the correct currency id set (make sure this is a currency id too, not a country id by mistake), and then also check your order has it's currency id correctly set to the same currency.

    Matt

  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Sep 08, 2020 @ 11:26
    Søren Kottal
    0

    Thanks Matt,

    That seems simple enough :)

    I found my problem, I hadn't registered my ProductAdapter correctly, I was missing using Umbraco.Core, and mistakenly written composition.RegisterUniqueFor<>

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Sep 08, 2020 @ 13:20
    Matt Brailsford
    0

    Ahhh, yea.

    I keep getting caught out by that myself. There are a lot of extensions in Umbraco.Core (might the same with Vendr.Core) so it always pays to add a using statement for them, but it's so easy to forget.

    Glad you got it working though 👍

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft