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>();
}
}
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?
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 🤔
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?
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.
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)
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:
snapshot:
[ComposeAfter(typeof(VendrComposer))]
Hi Arne,
Hmm, it looks OK to me. If you set a breakpoint in your adapter does it ever get called?
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:
Then I try to print out the name like this:
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?
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 🤔
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?
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.
Ahhh, shoot. I think I might now what the problem is.
You might need to override the other
GetProductSnapshot
method that has aproductVariantReference
parameter. In the core, the one without this param just proxies to the other settingproductVariantReference
tonull
so you should really override the more complete version and just expect thatproductVariantReference
can benull
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
forproductVariantReference
if it's not a variant lookup. (I should probably add anObsoleteAttribute
to the shorter method)Nice, this sounds like a solution. I will try this and report back afterwards
Thanks again
I now get en error:
I changed the override adapter to:
and added ProductVariantReference to the snapshot:
Do I have to make some other canges?
The error occures when I try to get a product by the VendrApi:
Hi Arne,
What's the stack trace?
Infinite loop, my mistake, of course I have to call the base method to avoid infinite recursion
Haha, oops, yea, that would do it 😁
Does this mean it's now working for you?
Yes, it's now working.
Thanks for the quick and good support
Excellent! and no problem 👍
is working on a reply...