Developer forum

Forum » Ecommerce - Standard features » Managing the cart from an API perspective

Managing the cart from an API perspective

Søren Heide Larsen
Søren Heide Larsen
Reply

Hi,

We are currently working with web api for managing the basket, however, we are facing some issues in getting the discount correct, or at least it feels a bit messy and does not work proberly with vouchers.

What we do is the following:

1) Manipulate the orderlines, e.g. add orderline and trigger order line added notification

2) Recalculate the cart with the following code:

// We need to save the cart first in order to calculate discounts
// This needs to be done as discounts puses ParentLineId, which is first generated upon save
cart.Save();

// find force recalculate protected field
var field = cart.GetType().GetProperty("ForceCalculate", BindingFlags.Instance | BindingFlags.NonPublic);
if(field != null) field.SetValue(cart,true);
cart.ForcePriceRecalculation();
cart.CalculateDiscounts();

Any tips to making this better and making vouchers work?

Best Regards
Søren


Replies

 
Viktor Letavin
Reply

Hi Soren,

Could you please provide some more information about the way you working with basket because depending on this order calculation proccess may vary.

Please check what Dynamicweb.Environment.ExecutingContext.IsFrontEnd() method returns. If it return true then folowong code sample working correctly:


var order = new Order();

var builderConfig = new OrderLineBuilderConfig { ProductId = "Prod39", Quantity = 1, AddToCart = true };
var orderLine = order.OrderLineBuilder(builderConfig);

//the code for discount based on voucher
order.VoucherCode = "e785b5fe";

//see no special reason to call this
//order.ForcePriceRecalculation();
order.CalculateDiscounts();
order.Save();


 

If for some reason ExecutingContext.IsFrontEnd() returns false then you need to set ForceCalculate field to true(the way you do) before CalculateDiscounts method call.

 

Best regards, Viktor.

 

You must be logged in to post in the forum