Developer forum

Forum » Development » Add orderline using API

Add orderline using API

Lars Larsen
Lars Larsen
Reply

Hi

In the notification "BeforeRenderingNewStep" I need to add an orderline to the cart. The orderline must be with an existing product but the price of the product is variable and must therefore be set for the added orderline. I have tried a lot of different things to add this orderline but can get it right. How can this be done? 

Running DW 9.9.1


Replies

 
Lars Larsen
Lars Larsen
Reply

Hi

This is one of my failing attempts:

var specialProd = Dynamicweb.Ecommerce.Services.Products.GetProductById("PROD224", "", args.Order.LanguageId, false);
var priceInfo = new Dynamicweb.Ecommerce.Prices.PriceInfo(args.Order.Currency);
priceInfo.PriceWithVAT = 100;
priceInfo.PriceWithoutVAT = 80;
priceInfo.VAT = 20;
priceInfo.VATPercent = 25;

var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(args.Order, specialProd, 1, null, priceInfo);
var os = new Dynamicweb.Ecommerce.Orders.OrderLineService();

os.SetUnitPrice(ol, priceInfo);

Dynamicweb.Ecommerce.Services.Orders.Save(args.Order);

The orderline is added to the cart, but the price is not set to 100.

 

 
Mario Santos Dynamicweb Employee
Mario Santos
Reply

Hi Lars,

Have you tried to set ol.AllowOverridePrices = true before the line SetUnitPrice method?

BR, Mario
 

 
Lars Larsen
Lars Larsen
Reply

Hi Mario

Thanks for your input. When adding your suggestion my code looks like this:

var specialProd = Dynamicweb.Ecommerce.Services.Products.GetProductById("PROD224", "", args.Order.LanguageId, false);
var priceInfo = new Dynamicweb.Ecommerce.Prices.PriceInfo(args.Order.Currency);
priceInfo.PriceWithVAT = 100;
priceInfo.PriceWithoutVAT = 80;
priceInfo.VAT = 20;
priceInfo.VATPercent = 25;

var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(args.Order, specialProd, 1, null, priceInfo);
var os = new Dynamicweb.Ecommerce.Orders.OrderLineService();

ol.AllowOverridePrices = true;
os.SetUnitPrice(ol, priceInfo);

Dynamicweb.Ecommerce.Services.Orders.Save(args.Order);

But now the price is 0 for the added orderline.

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Lars,

The confusing bit here is that SetUnitPrice actually doesn't tell the orderline that it should use its own unit price. The orderline still thinks it needs to calculate the price. To get around that limitation, you need to set the orderline type to Fixed before setting the unit price. Doing that will tell the orderline to use the specified unit price instead of calculating it. Alternatively, you could use SetUnitPrice(OrderLine, double, bool). This will handle VAT calculations based on the currency and country of the order and also changing the type as needed. Both options accomplish the same result, so it all comes down to whether you need to control VAT calculation or not. Note, that you should be careful in this regard especially if you wind up mixing country or currency information on the same order. This is avoided wholesale by using the overload with double.

Regarding your code example, I'm not really sure why you use the global OrderLineService (through Dynamicweb.Econnerce.Services.OrderLines) but then instantiate a new one right after. Also, AllowOverridePrices has very specific use-cases. It's purpose is to allow you to override the entire price calculation, not just unit price. With AllowOverridePrices set to true, you assume responsibility for all price related information which means you could end up with an orderline where unit price is 100 and a quantity of 5 with a total price of 87. It should be used very carefully and in very specific situations - mostly integration specific.

- Jeppe

 
Lars Larsen
Lars Larsen
Reply

Hi Jeppe

Thanks for your explanation. Yes my code above is a bit messed up. I see that now when you point it out. I had tried a lot of things and then you end up with code that's not always very nice. But now I have this piece of code that works:

var specialProd = Dynamicweb.Ecommerce.Services.Products.GetProductById("PROD224", "", args.Order.LanguageId, false);
var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(args.Order, specialProd, 1, null, null);

Dynamicweb.Ecommerce.Services.OrderLines.SetUnitPrice(ol, 100, true);
Dynamicweb.Ecommerce.Services.Orders.Save(args.Order);

Previously in earlier Dynamicweb versions I have added orderlines using the API and had no issues with that. Since then the API has been changed and expanded, which I find good. But this of course also makes it more complex, and much of it is not documented. It would be nice if there were some documention for the most basic use cases wink

 

You must be logged in to post in the forum