Developer forum

Forum » Development » How to apply customer specific information from a PriceProvider

How to apply customer specific information from a PriceProvider

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I have a project where the remote ERP determines if a product can be sold to the currently selected ship to country. For example, it can be that a customer can see / order a product when they ship to country X but not when they ship it to country Y. My intent is to use a PriceProvider for this as it has all the functionality to reach out to an ERP when displaying a single product or a list of products.

I am stuck with applying the information to the product in the user's context. In other words, I am missing functionality like we have in a TemplateExtender to add new tags so I can do something like this in my PriceProvider:

something.SetTag("CanOrderCurrentProduct", GetValueFromXmlResponse());

where GetValueFromXmlResponse returns true or false depending on the response from the ERP. With this code, in my template I could then do:

if (GetBoolean("CanOrderCurrentProduct"))
{
  // Show add to cart button
}
else
{
  <p>Sorry, this product can't be shipped to your current shipping country.</p>
}

Inside the (standard) PriceProvider I see code like this:

public virtual void FillProductValues(ProductInfo productInfo, Product product, double quantity)
{
    double? newStock = (double?)productInfo["Stock"];
    if (newStock.HasValue && Helpers.IsDifferentStock(product.Stock, newStock.Value))
    {
        product.Stock = newStock.Value;
        Services.Products.Save(product);
        productInfo["Stock"] = null;
    }
    ...

}
 
 
That saves the information back on the product. I can't do that as my information is specific to the specific user / ship to country / product combination.
Is there a way to return this transient data from a Price Provider to the UI somehow? I guess I could stick it in HttpContext.Current.Items which is what I'll do if there's no better alternative.

Thanks!

Imar


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Imar

It is definitlely not the job of a price provider - but I understand it is easy to get the information from ERP when getting the price. So I think the httpcontext approach is currently the best option - just be careful to make the code also run without being in the httpcontext when used for api or feed purposes. The price provider is called in many other contexts than rendering...

When saving the value in the httpcontext, you can create extension methods for the productViewmodel to retrieve that information again in the template.

The FillProductValues example from our live code is not very optimal by the way - it should have been a stocklevel provider and will be sometime in the future.

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

>> It is definitlely not the job of a price provider - but I understand it is easy to get the information from ERP when getting the price. 

I totally agree. But I'll rename it to PriceAndOtherProductInfoProvider to make me feel a little better :-) Thanks for confirming this is (at the moment) an acceptable approach.

>> just be careful to make the code also run without being in the httpcontext

Yep, thanks for the reminder!

Imar

 

You must be logged in to post in the forum