Copied to clipboard

Flag this post as spam?

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


  • Andrew 23 posts 156 karma points
    Jan 25, 2023 @ 13:29
    Andrew
    0

    Vendr - Issue adding a zero-priced product

    I am having issues trying to add a zero-priced product in an Umbraco v8 / Vendr 2.4.1 project.

    For this project we are allowing things like samples and brochures to be "purchased", but everything will be free, so the price will always be 0.

    I get the error "Vendr.Common.Validation.ValidationException: Can't add a Product to an Order where there is no price for the Order currency." which I believe is down to the validation on the standard product adapter.

    I have had a dig through the forums and the docs and I have added a custom Product adapter with no validation to try and rectify this, and added a custom class to return as an IProductSnapshot.

    public class PimProductAdapter : ProductAdapterBase
    {
        private readonly IUmbracoContextFactory _contextFactory;
    
        public PimProductAdapter(IUmbracoContextFactory contextFactory)
        {
            _contextFactory = contextFactory;
        }
        public new IProductSnapshot  GetProductSnapshot(Guid storeId, string productReference, string languageIsoCode)
        {
            using (var context = _contextFactory.EnsureUmbracoContext().UmbracoContext)
            {
                var node = context.Content
                    .GetById(Guid.Parse(productReference));
    
                return node.GetProductSnapshot(storeId);
            }
        }
    

    Product Snapshot:

    public class PimProductSnapshot : IProductSnapshot
    {
        public Guid StoreId { get; set; }
        public string ProductReference { get; set; }
        public string ProductVariantReference { get; set; }
        public string Sku { get; set; }
        public string Name { get; set; }
        public IEnumerable<AttributeCombination> Attributes { get; set; } = new List<AttributeCombination>();
        public Guid? TaxClassId { get; set; }
        public IEnumerable<ProductPrice> Prices { get; set; }
        public IDictionary<string, string> Properties { get; set; } = new Dictionary<string, string>();
        public bool IsGiftCard { get; set; }
    
    }
    

    and registered in the composition as so:

    public class DependencyComposer : ICoreComposer
    {
        public void Compose(Composition composition)
        {
            if (composition == null)
            {
                throw new ArgumentNullException(nameof(composition));
            }
    
            composition.RegisterUnique<IProductAdapter, PimProductAdapter>();
    

    However I am still getting the error, and a breakpoint on the custom adapter doesn't get hit. I have also tried inheriting from IProductAdapter rather than ProductAdapterBase.

    Any help on how to resolve this, or an alternative method of allowing 0 priced products would be appreciated, thanks.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 25, 2023 @ 14:19
    Matt Brailsford
    0

    Hi Andrew,

    I've not tried this, but rather than going through all this product adapter stuff, you could try removing the validation handler that checks there is price for given country.

    In a composer, you could do

    builder.WithValidationEvent<ValidateOrderProductAdd>()
        .Remove<ValidateProductAddHasPrice>();
    

    This should remove the check that ensure a price exists.

    By removing this, it could be that another problem now pops up, but as a first step to getting you past that error, it should do the trick.

  • Andrew 23 posts 156 karma points
    Jan 25, 2023 @ 14:46
    Andrew
    0

    Hi Matt

    Thanks for your quick reply. The solution is Umbraco 8 and is using a Composition for registering dependencies, I have just tried

    composition.WithValidationEvent<ValidateOrderProductAdd>().RemoveHandler<ValidateProductAddHasPrice>();
    

    and I'm still getting the same error. Could be the difference between this and the builder pattern but I imagine the two methods are doing the same thing?

    I'm wondering if it's something to do with the order the composition is being done? Is the user defined composer fired before or after the Vendr stuff? As this would explain why my custom ProductAdapter wasn't registering too if the Vendr dependencies are overwriting what I've done?

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 25, 2023 @ 14:50
    Matt Brailsford
    0

    Yea, I struggle to think back to the v8 API 😁

    And yea, order is important so you may need to add something like this above your composer class declaration

    [ComposeAfter(typeof(VendrWebComposer))]
    public class MyComposer : IUserComposer
    ....
    

    I think that is the right type for v8.

  • Andrew 23 posts 156 karma points
    Jan 25, 2023 @ 15:00
    Andrew
    101

    Luckily it's still pretty close!!

    Cracked it, the composer I was using was an ICoreComposer, which I assume is called pretty early and certainly before Vendr has registered anything.

    Added a new composer as below and it's working perfectly with no need for a custom ProductAdapter.

    [ComposeAfter(typeof(VendrComposer))]
    public class VendrDependencyComposer : IComposer
    {
        public void Compose(Composition composition)
        {
            composition.WithValidationEvent<ValidateOrderProductAdd>().RemoveHandler<ValidateProductAddHasPrice>();
        }
    }
    

    At least if this causes some knock on issues I know what was stopping my custom ProductAdapter from registering and I can revisit that.

    Really appreciate the help and thanks for getting this solved so quickly!

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 25, 2023 @ 15:05
    Matt Brailsford
    0

    No worries.

    Glad you got it working 👍

Please Sign in or register to post replies

Write your reply to:

Draft