Developer forum

Forum » Development » Preparing prices for multiple products

Preparing prices for multiple products

Dmitrij Jazel
Reply
Hej Guys,

I am trying to Preparing prices for multiple products here, I am following this post here:
http://developer.dynamicweb-cms.com/documentation-8.0/for-developers/%28moved%29-ecommerce/%28moved%29-extensibility/providers/price-providers.aspx

And having some difficulties with "MyPriceProviderCache" see the example below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Dynamicweb.eCommerce.Prices;
using Dynamicweb.Extensibility;
using Dynamicweb.Frontend;
using System.Data.SqlClient;
using Dynamicweb.eCommerce.Products;


public class CustomDiscountPriceProvider : PriceProvider
{

    public override void PreparePrices(ProductCollection Products)
    {
        // Fetch prices from the external system using a mock connector
        Dictionary<string, double> productPrices = FindPricesHelper(Products);

        // Clear old cache
        if (HttpContext.Current.Items.Contains("MyPriceProviderCache"))
            HttpContext.Current.Items.Remove("MyPriceProviderCache");

        // Put new prices into cache
        HttpContext.Current.Items.Add("MyPriceProviderCache", productPrices);
    }

    private Dictionary<string, double> FindPricesHelper(ProductCollection Products)
    {
        Dictionary<string, double> returnProductPriceCombo = new Dictionary<string, double>();

        foreach (Product p in Products)
        {
            returnProductPriceCombo.Add(p.ID, 100);
        }

        return returnProductPriceCombo;
    }
}

What I basically want to do here, is that I want to set all products the same price no mater what. Simply 100 DKK before tax.
But that does not seem to work for some reason. No exception, just prices cannot be assigned to their products...
I feel it's cause MyPriceProviderCache or do I still also have to override this:
 public override PriceRaw FindPrice(Dynamicweb.eCommerce.Products.Product Product, double Quantity, string VariantID, Dynamicweb.eCommerce.International.Currency Currency, string UnitID, Dynamicweb.Frontend.Extranet User)


Any suggestions?

Regards,
Dmitrij





Replies

 
Jais Edelmann
Reply
 Wouldent it make more sense to create a discount type which makes all products cost 100 DKK
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Dmitrij

If you want your prices to be decided by your PriceProvider, you always have to override FindPrice. If you don't, you only ever fill the cache but never use it.

- Jeppe

 
Dmitrij Jazel
Reply
Hej Jais,

Well in this way I am testing the app, cause I to make sure that method works the I want it. Instead of 100 DKK there will be a calculation method call. This calculation method will calculate a custom price for every single user.

Jeppe, yes that is also what I thought, But in that post they are saying this:

"Consider the situation where a list of products is about to be rendered and you need to get prices for all of those. If your prices are stored in an external system like an ERP such as Microsoft Dynamics NAV® and you have to get the price of one product at a time, that would be a huge performance drain.
There is a way to handle this more cleverly."

And that is exactly the case, I am experiencing a lack of performance here, and I want to fix it.
As you remember some time ago I was making the custom discount method, well this time I just want to do simmilar thing, but I must speed it up.
back than you told I should just use FindPrice method that is totally understandable, so that I can extract and override price for each product DW passes me as parameter. But now I am not sure how I could use PreparePrice along with FindPrice to speed it up a little bit?

Regards,
Dmitrij

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer
>> But now I am not sure how I could use PreparePrice along with FindPrice to speed it up a little bit?

I think it should be like this:

1. Override PreparePrices, go into your data source, get a bunch of prices and store them in a cache you control.

2. Override FindPrice, and retrieve the price from your cached data.

This way you can do batch retrieval of prices in PreparePrices, and still return individual prices in FindPrice.

Hope this helps,

Imar

Votes for this answer: 0
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

The very next paragraph in the documentation, inserted here, clearly states that PreparePrices and FindPrice go hand in hand--I've marked the relevant parts in bold.
 

Using PreparePrices

The PriceProvider base class contains an overridable method called PreparePrices. This method has two different signatures and in this section we'll take a look at the PreparePrices(ProductCollection) overload. The second overload, PreparePrices(Dictionary), is discussed later.
PreparePrices is used to notify the PriceProvider that it soon will receive prices requests for the products in the collection. This gives the PriceProvider a chance to bulk fetch the prices from the external system and cache them. Then when FindPrice is called the PriceProvider needs only check its cache to find the price.

 

 
Dmitrij Jazel
Reply
 Hej Imar and Jeppe,

Thanks alot for explanation, now I see, that you actually have to cache the "stuff to prepare price" and than simply apply it, and than apply it one by one to the product. I thought about another way around, but thanks alot for explanation :-)

Regards,
Dmitrij


 

You must be logged in to post in the forum