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.
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.
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?
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!
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.
Product Snapshot:
and registered in the composition as so:
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.
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
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.
Hi Matt
Thanks for your quick reply. The solution is Umbraco 8 and is using a Composition for registering dependencies, I have just tried
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?
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
I think that is the right type for v8.
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.
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!
No worries.
Glad you got it working 👍
is working on a reply...