Developer forum

Forum » Development » Setting custom unit price

Setting custom unit price

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I have a notification subscriber for Cart.Line.Increased. Inside the OnNotify method I have the following code:

double price = repo.GetPriceFromCudFile(addedSessionId);
item.AllowOverridePrices = true;
item.Price.Currency = increasedArgs.Cart.Currency;
item.Quantity = increasedArgs.IncreasedLine.Quantity;
item.SetUnitPrice(price);
item.Price.Currency = increasedArgs.Cart.Currency;
item.Price.PriceWithoutVAT = price;
item.Price.PriceWithVAT = price;
item.Price.VATPercent = 0;
item.Save();

The subscriber runs fine but it doesn't multiply the line price by the quantity. Should I calculate the total myself and pass that to SetUnitPrice? Or is something else going on?

Thanks,

Imar


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I think I just answered my own question. I found this Price property on the OrderLine:

public PriceInfo Price
{
  get
  {
  if (!this.AllowOverridePrices)
  {
    if (this.Order.Calculate)
    {
    return this.UnitPrice.Multiply(this.Quantity);
    }
    if ((this._Price.Price == 0.0) & (this.Type == Base.ChkString((int) Base.ChkNumber(OrderLineType.Discount))))
    {
    return this.UnitPrice.Multiply(this.Quantity);
    }
  }
  return this._Price;
  }
}

The check for AllowOverridePrices seems to suggest that I am indeed responsible for the multiplication. Is this by design and should I just calculate the total myself?

Thanks,

Imar

 
Nicolai Høeg Pedersen
Reply

Hi Imar

Yes, that is by design. The moment you set the price all price calculation made by DW needs to keep hands off. You do set the price on the orderline in your example - you do not set the price on the product - then Dynamicweb would multiply for you. When you explicitly set the price on the orderline, that is the price of the orderline.

BR Nicolai

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I see, thanks for clarifying that. Would it be better to set it in the product instead?

Imar

 
Nicolai Høeg Pedersen
Reply

It depends on if it is the general price of the product or the price in the context of the cart you are modifying.

You could use a price provider to control the price of the product - i.e. this and that customer has this price for this product. Then it will also show the right price in product lists, product details etc. Using your approach above is suitable for situations where the price is only affected in the context of the cart.

BR Nicolai

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I think it's just the price of the item in the cart that I should be updating. Here's the situation:

1. We use the Guru configurator to configure a product. In Ecommerce, the Product is merely a "Configurable Product X" or "Configurable Product Y" (there are about 20 base systems)

2. Based on Guru, we build a configuration which essentially results in an XML document. That gets stored in the database, and a reference to its unique ID is stored as an order line field. That allows us to have multiple products of the same type in the cart, each with a different price

3. When we add or update a product in the cart, we calculate the price for that specific instance (and not the others of the same type in the cart) based on the XML document (which contains many nodes with prices for the selected components that make up the configuration)

Based on that, it seems that a subscriber Line.Added and Line.Increased to override the price with the code below seems to make the most sense. Is that correct?


The only thing I don't like is that I lose the original price when I am sending off this configuration to various ERP backends; but I guess that's just a matter of dividing the line price again by the quantity.

Imar

 
Remi Muller
Reply

Back to your original question can't you use this code?

var priceinfo = new PriceInfo();
priceinfo.Currency = Context.Currency;

var pricecalculated = new PriceCalculated();
pricecalculated.IgnoreRounding = true; //use a higher precision than 2 decimals for calculating the unitprice
pricecalculated.PriceRaw = new PriceRaw(Convert.ToDouble(newprice), Context.Currency);

orderline.Type = Base.ChkString(Base.ChkNumber(Dynamicweb.eCommerce.Orders.OrderLine.OrderLineType.Fixed));
orderline.SetUnitPrice(priceinfo.Add(pricecalculated));

 

newprice is the new unit price and it still calculates the total for the orderline with the quantity

 

You must be logged in to post in the forum