Developer forum

Forum » Development » Create orderline by API - but missing orderline total

Create orderline by API - but missing orderline total

Lars Larsen
Lars Larsen
Reply

Hi

I'm trying to create an order line by the API. I have done this:

            var product = Dynamicweb.Ecommerce.Services.Products.GetProductById("95764469898", string.Empty, Context.LanguageID, false);
            var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(order, product, 2.0, null, null);

            ol.OrderLineType = OrderLineType.Fixed;

            Dynamicweb.Ecommerce.Services.OrderLines.SetUnitPrice(ol, 240.00, true);  
            Dynamicweb.Ecommerce.Services.Orders.ForcePriceRecalculation(order);
            Dynamicweb.Ecommerce.Services.Orders.Save(order);

But there is no orderline total on the order:

How do I get the orderline total?

 

 


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

I think we need a bit more code and context.

Where do you create this object? Is it in frontend or backend - and is the order object you use a cart or an order?

If the order is an order, it is no longer calculated.

If you are also using live integration, it could lock the orderline as being calculated by that.

You might also be able to move ol.OrderLineType = OrderLineType.Fixed; to after you have set the unmitprice and forced the calculation and accessed the orderline.price property. But only if it is a cart and if the orderline has not been marked as AllowOverridePrices = true from an integration...

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai

The context is frontend. The use case is we need to programmatically create an order with out completing a checkout flow. I got this code now:

            var order = new Order(Context.Currency, Context.Country, Context.Language);
            var product = Dynamicweb.Ecommerce.Services.Products.GetProductById("9788799045686", string.Empty, Context.LanguageID, false);

            order.IsCart = true;

            var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(order, product, 2.0, null, null);

            ol.OrderLineType = OrderLineType.Fixed;

            Dynamicweb.Ecommerce.Services.OrderLines.SetUnitPrice(ol, 249.00, true);

            order.Complete = true;

            Dynamicweb.Ecommerce.Services.Orders.Save(order);

The order line total and all other prices in the cart are correct. But I can't figure out how to make the cart into an order. I have tried setting order.IsCart=false just before saving the order (last line above) but then the order doesn't contain an order line total nor order total. How can I turn the cart into a completed order using the API and keep the totals?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

When you set order.complete = true, Dynamicweb will stop calculating.

So right after Dynamicweb.Ecommerce.Services.OrderLines.SetUnitPrice(ol, 249.00, true); you should access Orderline.price

I.e. 

var orderlineprice = ol.price;

 
Lars Larsen
Lars Larsen
Reply

I finally found the method Dynamicweb.Ecommerce.Services.Orders.UpdateCartToOrder() which did the job. If other are interested, here is the final code:

            var order = new Order(Context.Currency, Context.Country, Context.Language);
            var product = Dynamicweb.Ecommerce.Services.Products.GetProductById("9788799045686", string.Empty, Context.LanguageID, false);

            order.IsCart = true;

            var ol = Dynamicweb.Ecommerce.Services.OrderLines.Create(order, product, 2.0, null, null);

            ol.OrderLineType = OrderLineType.Fixed;

            Dynamicweb.Ecommerce.Services.OrderLines.SetUnitPrice(ol, 249.00, true);

            order.Complete = true;

            Dynamicweb.Ecommerce.Services.Orders.Save(order);
            Dynamicweb.Ecommerce.Services.Orders.UpdateCartToOrder(order, false);

 

You must be logged in to post in the forum