I'm trying to implement a custom price provider that calculates the price according to a specified unit.
We have a custom table containing the information how to calculate from one unit to another:
ProductId | FromUnit | ToUnit | Factor |
1 | piece | m2 | 278 |
1 | piece | package | 3000 |
... | ... | ... | ... |
As far as I can see, Dynamicweb doesn't support converting between units using a factor like above. So now I'm trying to solve this by implementing Dynamicweb.Ecommerce.Prices.PriceProvider.
I see the method FindPrice already receives a unitId parameter. So my first attempt was the following (simplified) implementation:
public override PriceRaw FindPrice(Product product, double quantity, string variantId, Currency currency,
string unitId, User user)
{
var defaultProvider = new DefaultPriceProvider();
var price = defaultProvider.FindPrice(product, quantity, variantId, currency, unitId, user);
if (string.IsNullOrEmpty(variantId)) return price;
var defaultUnit = product.DefaultUnitId;
var unitFactor = UnitService.Instance.GetUnitConversionFactor(int.Parse(product.Number), defaultUnit, unitId);
price.Price *= unitFactor;
return price;
}
But thusfar I cannot find a way to call this method (from a template) using a specific unitId.
I tried:
@foreach (var unit in GetLoop("Units")) {
@unit.GetValue("Ecom:VariantOption.Price.Price")
}
This does call FindPrice multiple times, but unitId is always empty.
Even if the above would call FindPrice with a unit, the loop only contains items based on the stock matrix if I'm not mistaken. And in our case we don't manage stock information in Dynamicweb, so I'd rather not have to populate these tables in order to get the price for a specific unit.