Posted on 30/12/2022 11:48:30
Hi Tomas,
Unfortunately this is still not available. There is a workaround though.
Considerations
- The ViewModels (hence the webapi as well) don't get the Live Prices updated automatically
- This also means that in Swift, the lazy loading of data will be useless - the workaround will make the request to the erp on page request
This also means if the ERP is slow to respond, the webpage will take a bit longer (not feel zippy since it's held hostage by the ERP request/response)
This will be noticeable if you apply the workaround to templates used in a product list
C# method
I'd recommend you put this into a dll. This method updates the ViewModel Prices collection with the Live Integraton response values.
public static void UpdateProductViewModelPrices(ProductViewModel product)
{
if (!Dynamicweb.Ecommerce.DynamicwebLiveIntegration.IsWebServiceConnectionAvailable())
{
return;
}
var productIdentifierKey = Dynamicweb.Ecommerce.LiveIntegration.Examples.CustomProductProvider.GetProductIdentifier(product.Id, product.VariantId, product.LanguageId, product.Number, product.DefaultUnitId);
var productInfo = Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products.ProductManager.GetProductInfo(productIdentifierKey);
if (productInfo == null)
{
Dynamicweb.Ecommerce.LiveIntegration.TemplatesHelper.UpdateProduct(product.Id, product.VariantId, 0, product.DefaultUnitId, true);
productInfo = Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products.ProductManager.GetProductInfo(productIdentifierKey);
if (productInfo == null)
{
return;
}
}
var totalPrice = Dynamicweb.Core.Converter.ToDouble(productInfo["TotalPrice"]);
product.Price.Price = totalPrice;
product.Price.PriceFormatted = Dynamicweb.Ecommerce.Services.Currencies.Format(Dynamicweb.Ecommerce.Common.Context.Currency, totalPrice);
var productInfoPrices = ((IList<ProductPrice>)productInfo["Prices"] ?? Enumerable.Empty<ProductPrice>()).Where(p =>
(p.ValidFrom == null || p.ValidFrom.Value <= DateTime.Now.Date) &&
(p.ValidTo == null || p.ValidTo.Value >= DateTime.Now.Date) &&
((p.UnitId ?? "") == "" || (p.UnitId ?? "") == (product.DefaultUnitId ?? ""))).ToList();
if (!productInfoPrices.Any()) return;
product.Prices.Clear();
foreach (var productInfoPrice in productInfoPrices.Where(productInfoPrice => productInfoPrice.Amount != null && productInfoPrice.Quantity != null))
{
var amount = productInfoPrice.Amount ?? 0;
product.Prices.Add(new PriceListViewModel {
Price = new PriceViewModel {
Price = amount,
PriceFormatted = Dynamicweb.Ecommerce.Services.Currencies.Format(Dynamicweb.Ecommerce.Common.Context.Currency, amount)
},
Quantity = productInfoPrice.Quantity ?? 0
});
}
}
Templates
In the templates all you need to do it
Your.Custom.Assembly.UpdateProductViewModelPrices(product);
isLazyLoadingForProductInfoEnabled = false;
The first line updates the ViewModel; The second line should update all of the other template logic to work accordingly (because this does not work with lazy loading of live integration data).
These lines need to be applied after both the "product" variable and the "isLazyLoadingForProductInfoEnabled" variable are defined. Ideally if this is fixed in the near future, you simply need to delete these 2 lines and you are good to go.
Final notes
The C# methods I got you were slightly altered from what we use, because we created overloads under the same class (in our project) so it was easier to use and understand. Nonetheless, I looked up in DW's source code and I believe I got the right namespaces and classes for all the methods. The method names are accurate though.
Hope this helps.
Best Regards,
Nuno Aguiar