Developer forum

Forum » Development » A challenge with prices

A challenge with prices

Martin Nielsen
Reply

Hi DW,

I trying to build a somewhat special logic regarding prices, and i'm having problems getting everything to work properly.

Intro

In Dynamicweb we have around 600 products and each of these products represent a mother product, which means that these products do not make much sense on their own, but combined with another information the are complete.

The additional information that is needed for a product to be complete, can be found in an external service that we hook up to on-the-fly and this service returns information about a batchnumber. 

To find the correct price you need the productnumber from Dynamicweb, the Batchnumber from the external service and a customernumber.
When i have these 3 values i can ask another webservice for a price for this specific batch og products.

 

Example data

In Dynamicweb eCommerce

ProductID ProductName
P00123 Product 1
P00456 Product 2

 

In the webservice

ProductID BatchNumber
P00123 B01
P00123 B02
P00456 B03
P00456 B04
P00456 B05

 

My issue is that the PriceProvider in Dynamicweb doesn't know about the BatchNumber since it's not part of the product. And at the moment the value only exists as an OrderLineFieldValue on my order.

 

I've solved the problem 50% by not using the priceprovider at all, but i'm having big problems getting the correct price rendered in my cart.

 

So far i have an OrderExtender that isn't working with this code:

 

      if( RenderingState == TemplateExtenderRenderingState.Before )
      {

        Order order = base.Order;
        order.AllowOverridePrices = true;

        foreach( OrderLine orderLine in order.OrderLines )
        {
          /* OUTLET: Override unitprice with value from service*/
          OrderLineFieldValue ofv = orderLine.get_OrderLineFieldValue( "batchnumber" );
          string batchNumber = string.Empty;
          if( ofv != null && ofv.Value != null )
          {
            batchNumber = ofv.Value;
          }

          if( batchNumber.Length > 0 )
          {
            /* Ocerride unit price on outlet products */
            PriceRaw pr = DataHelper.GetPriceFromService( orderLine.Product.Number, batchNumber, base.User.CustomerNumber, "1", Dynamicweb.eCommerce.Common.Context.Currency );
            PriceCalculated pc = new PriceCalculated( orderLine.Product, pr );

            orderLine.AllowOverridePrices = true;
            orderLine.SetUnitPrice( pc, true );
          }
        }
        order.Save();
      }

 

 


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi Martin,

The reason this does not work is because of the way you use AllowOverridePrices. When you set this property, you need to manipulate the orderline.Price object directly. Basically, setting AllowOverridePrices = true is a way to access the internal PriceInfo object for the orderline and this price is then not calculated based on the UnitPrice.

Something like this:

PriceRaw pr = DataHelper.GetPriceFromService( orderLine.Product.Number, batchNumber, base.User.CustomerNumber, "1", Dynamicweb.eCommerce.Common.Context.Currency );
PriceCalculated pc = new PriceCalculated( orderLine.Product, pr );

orderLine.AllowOverridePrices = true;
orderLine.Price.PriceWithVAT = pc.PriceWithVAT * orderLine.Quantity;
orderLine.Price.PriceWithoutVAT = pc.PriceWithoutVAT * orderLine.Quantity;
orderLine.Price.VAT = orderLine.PriceWithVAT - orderLine.PriceWithoutVAT;

Also, you do not need to set order.AllowOverridePrices = true unless you plan to override the price on the order to something different that the total price of the orderlines.

- Jeppe

Votes for this answer: 1
 
Martin Nielsen
Reply

Hi Jeppe,

Great - Works pretty good.

It doesn't seem to affect the UnitPrice. Can i override this for the current orderline aswell?

I changed my code to this:

 /* Override unit price on outlet products */
            PriceRaw pr = DataHelper.GetPriceFromMovex( qualityNumber, batchNumber, base.User.CustomerNumber, "1", Dynamicweb.eCommerce.Common.Context.Currency );
            PriceCalculated pc = new PriceCalculated( orderLine.Product, pr );

            orderLine.AllowOverridePrices = true;
            orderLine.Price.PriceWithVAT = pc.PriceWithVAT * orderLine.Quantity;
            orderLine.Price.PriceWithoutVAT = pc.PriceWithoutVAT * orderLine.Quantity;
            orderLine.Price.VAT = orderLine.Price.PriceWithVAT - orderLine.Price.PriceWithoutVAT;

And removed order.AllowOverridePrices

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Well, I'm actually not sure you need AllowOverridePrices at all. Have you tried not setting that and just setting the UnitPrice?

Like this:

PriceRaw pr = DataHelper.GetPriceFromMovex( qualityNumber, batchNumber, base.User.CustomerNumber, "1", Dynamicweb.eCommerce.Common.Context.Currency );
PriceCalculated pc = new PriceCalculated( orderLine.Product, pr );

orderLine.SetUnitPrice( pc, true );

If that does not work, then you need to keep you code as it is and add the following after you've set the prices in order to set the UnitPrice correctly:

orderLine.SetUnitPrice( pc );
orderLine.Type = ((int)OrderLineType.Fixed).ToString();
Votes for this answer: 1
 
Martin Nielsen
Reply

Adding: orderLine.Type = ((int)OrderLineType.Fixed).ToString(); corrected the unitprice.

 

You must be logged in to post in the forum