Copied to clipboard

Flag this post as spam?

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


  • Arne 15 posts 68 karma points
    Oct 23, 2021 @ 15:18
    Arne
    0

    Product prices from external source

    Hi

    I am trying to override prices, product names and sku in Umbraco with data from an external source. I have already created an OrderLineCalculatorto that works well, but I have problems overriding the fields in the solution with a product adapter. I have created a product adapter/ProductSnapshot and registered this in the composer, but this does not work. I am not getting any errors, but the product values are still from Umbraco. See my code below

    (Umbraco v8.17 and Vendr v.2.0.2)

    Appreciates all the help that can lead me in the right direction.

    Adapter:

    public class ProdAdapter : UmbracoProductAdapter
    {
    
        public ProdAdapter(IUmbracoContextFactory contentFactory, IContentService contentservice, PublishedContentWrapperFactory contentWrapperFactory)
        : base(contentFactory, contentservice, contentWrapperFactory)
        { }
    
        public override IProductSnapshot GetProductSnapshot(string productReference, string languageIsoCode)
        {
            var snapshot = GetProductSnapshot(productReference, languageIsoCode); 
    
            return snapshot != null
                ? new VendrCustomProductSnapshot(snapshot)
                : null;
        }
    
    
    }
    

    snapshot:

    public class VendrCustomProductSnapshot : ProductSnapshotBase
    {
        private readonly IProductSnapshot _snapshot;
    
        public VendrCustomProductSnapshot(IProductSnapshot snapshot)
        {
            _snapshot = snapshot;
        }
    
    
        public override string Name => "testing";
        public override string Sku => "123"; // _snapshot.Sku;
    
        public override Guid StoreId => _snapshot.StoreId;
    
        public override string ProductReference => _snapshot.ProductReference;
    
        public override Guid? TaxClassId => _snapshot.TaxClassId;
    
        public override IEnumerable<ProductPrice> Prices => _snapshot.Prices;
    
        public override IDictionary<string, string> Properties => _snapshot.Properties;
    
        public override bool IsGiftCard => _snapshot.IsGiftCard;
    
        public override string ProductVariantReference => throw new NotImplementedException();
        public override IEnumerable<AttributeCombination> Attributes => _snapshot.Attributes;
    
    }
    

    [ComposeAfter(typeof(VendrComposer))]

    public class Composer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.RegisterUnique<IProductAdapter, ProdAdapter>();
        }
    }
    
  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Oct 25, 2021 @ 08:05
    Matt Brailsford
    0

    Hi Arne,

    Hmm, it looks OK to me. If you set a breakpoint in your adapter does it ever get called?

  • Arne 15 posts 68 karma points
    Oct 25, 2021 @ 09:44
    Arne
    0

    Thanks for your reply

    The adapter override is never calld. Perhaps I am using it wrong?

    I have set up a test page where I get the product this way:

    var product = VendrApi.Instance.GetProduct (Model.Key.ToString (), "nb-NO");
    

    Then I try to print out the name like this:

    @product.Name
    

    I then suppose the name showing on the page should be the AdaperOverride name and not the Umbraco node name? Isnt that right?

    My main goal is to override the prices so I dont have to add the price property in umbraco. Is my plan by doing this with and ProductAdapter override in combinatin with the orderLineCalculater the right way to go?

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Oct 25, 2021 @ 10:00
    Matt Brailsford
    0

    Hi Arne,

    Yea, that should all work as far as I can tell. I can't think why replacing the adapter wouldn't take effect. The order of the composer is usually the culprit but you have the ComposeAfter attribute so this should be taken care of.

    ProductAdapter is generally the right way to go to swap all this stuff out for custom sources so this should be doing what you need 🤔

  • Arne 15 posts 68 karma points
    Oct 25, 2021 @ 11:11
    Arne
    0

    Thanks again, good to know I am on the right track.

    My project is an upgrade from version 1.8, can this be a problem?

    The OrderLineCalculator is working, so I can make my own contoller to handel the prices in the views. Is there another way I can add products to the cart without having a price property on the product node in Umbraco?

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Oct 25, 2021 @ 12:46
    Matt Brailsford
    0

    Hmm, very weird.

    This is definitely the recommended approach.

    I'm going to have to try and setup the demo store with a custom product adapter to see it's not triggering. It looks like everything is in order so it really should be working.

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Oct 25, 2021 @ 12:53
    Matt Brailsford
    101

    Ahhh, shoot. I think I might now what the problem is.

    You might need to override the other GetProductSnapshot method that has a productVariantReference parameter. In the core, the one without this param just proxies to the other setting productVariantReference to null so you should really override the more complete version and just expect that productVariantReference can be null meaning it's just a parent product request.

    We kept the outer one for backwards compatibility reasons, in case anyone was using it in their own code, but I don't think we actually call that from within our code. We always call the more expanded method passing null for productVariantReference if it's not a variant lookup. (I should probably add an ObsoleteAttribute to the shorter method)

  • Arne 15 posts 68 karma points
    Oct 25, 2021 @ 13:10
    Arne
    0

    Nice, this sounds like a solution. I will try this and report back afterwards

    Thanks again

  • Arne 15 posts 68 karma points
    Oct 25, 2021 @ 14:44
    Arne
    0

    I now get en error:

    'System.StackOverflowException' was thrown.'
    

    I changed the override adapter to:

        public override IProductSnapshot GetProductSnapshot(string productReference, string productVariantReference,  string languageIsoCode)
        {
            IProductSnapshot snapshot = GetProductSnapshot(productReference, productVariantReference,  languageIsoCode);
    
    
            return snapshot != null
                ? new VendrCustomProductSnapshot(snapshot)
                : null;
        }
    

    and added ProductVariantReference to the snapshot:

      public override string ProductVariantReference => _snapshot.ProductVariantReference;
    

    Do I have to make some other canges?

    The error occures when I try to get a product by the VendrApi:

    VendrApi.Instance.GetProduct

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Oct 25, 2021 @ 14:51
    Matt Brailsford
    0

    Hi Arne,

    What's the stack trace?

  • Arne 15 posts 68 karma points
    Oct 25, 2021 @ 14:57
    Arne
    0

    Infinite loop, my mistake, of course I have to call the base method to avoid infinite recursion

        public override IProductSnapshot GetProductSnapshot(string productReference, string productVariantReference,  string languageIsoCode)
        {
            IProductSnapshot snapshot = base.GetProductSnapshot(productReference, productVariantReference,  languageIsoCode);
    
    
            return snapshot != null
                ? new VendrCustomProductSnapshot(snapshot)
                : null;
        }
    
  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Oct 25, 2021 @ 15:25
    Matt Brailsford
    0

    Haha, oops, yea, that would do it 😁

    Does this mean it's now working for you?

  • Arne 15 posts 68 karma points
    Oct 25, 2021 @ 15:27
    Arne
    0

    Yes, it's now working.

    Thanks for the quick and good support

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Oct 25, 2021 @ 15:35
    Matt Brailsford
    0

    Excellent! and no problem 👍

Please Sign in or register to post replies

Write your reply to:

Draft