Developer forum

Forum » Development » Manipulate Order object total in Cart.CheckoutDone subscriber

Manipulate Order object total in Cart.CheckoutDone subscriber

Dimitrij Jazel
Reply

Hi guys,

THis is URGENT, Appreciate your understanding!

I need to be able to add a custom discount orderLine to OrderObject. I must do that After order is complete.

Why you might ask? My collegue already tried changing Cart object in the OnOrderLineAdded event, and so far BEFORE order hits payment gateway - solutions seems to do exactly what we need.

But as soon as Cart becomes Order, it ignoves a custom discount orderline that was added.

That is why the easyest way out would be ofcourse to manipulate order object after it passed paymeng gateway, but before it is saved in the db, etc... that would be ideal.

 

Here is my example so far:

double quantityDiscountAmount = RemoteService.CalculateDiscount(); // Getting SpecialDiscount from ERP
order.AllowOverridePrices = true;
var name = string.Format("ERP-Discount-{0}", order.ID);
B2BLogging.CreateDiscountOrderline("Creating new and setting");
var discountOrderline = new OrderLine();
discountOrderline.Order = order;
discountOrderline.Quantity = 1;
discountOrderline.ProductName = name;
discountOrderline.SetOrderLineType(OrderLine.OrderLineType.Discount);
discountOrderline.AllowOverridePrices = true;
discountOrderline.SetUnitPrice(Base.ChkDouble(quantityDiscountAmount));
discountOrderline.Price.PriceWithoutVAT = Base.ChkDouble(quantityDiscountAmount);
discountOrderline.Price.PriceWithVAT = Base.ChkDouble(quantityDiscountAmount);
order.OrderLines.Add(discountOrderline);
discountOrderline.Save();
discountOrderline.ClearCachedPrices();
order.AllowOverridePrices = true;
order.ClearCachedPrices();                
order.Save();

After order is done, Orderline is added in order overview, with correct price, but total price is not including the discount.

This code works really nicely while in Cart. 

 

I also tried this order DowngradeToCart() and UpdateCartToOrder() again. But this did not help either.

This is the result. https://www.screencast.com/t/Ct5e6Rtz7jKR

Line is added, but line price is not calculated along in total.

 

/Dmitrij

 


Replies

 
Nicolai Pedersen
Reply

Why are you adding a discount line? That is the job og the discount manager and it gets confused when you do this (it is a blonde girl, right). Why not use a real discount? Or use a regular orderline with a negative amount in it...

 
Dimitrij Jazel
Reply

Hi Nicolai,

Understand and totally agree with you.

And we actually trying to use exact same thing you just mentioned here "Or use a regular orderline with a negative amount in it..."

 

After I made a quick search on Forum, I found quite a few things that we should have done differently. But the guy (author of this whole thing) is on Hollidays, and everything works like clock, we need to keep this in this way. Redoing this would be too big risky, and noone would allow me to do that.  Hope you understand the situation.

 

In the code example that I posted in my previous post, am I adding "negative amount" in wrong way?

The problem is that neigher amount gets added, it just gets ignored. Though orderline is actually displayed when order is complete.

 
Nicolai Pedersen
Reply

Hi Dimitrij

I understand - but I cannot change how the calculation of an order works to do this. As far as I  know, it is not possible to make a change to the order at this step, and especially discounts cannot be added dynamically like this... You need a discount provider to do that. So you need to find a way to make it work - I have no clue how.

BR Nicolai

 
Dimitrij Jazel
Reply

Hi Nicolai,

Well, atleast I got an answer :) not just plain silance.

Thanks for that.

 

 

But speaking of this order wouldn't it be an option, to add this discount before Cart reaches the checkout?

Maybe my collegue is applying discount in a bad way?

This is what he does (in ProductLineAdded notification):

var discountOrderline = order.OrderLines.First(x => x.ProductName.Contains("ERP-Discount"));
double quantityDiscountAmount = RemoteService.CalculateDiscount(); // This already receives Negative amount "-105.54" etc...
                    var name = string.Format("ERP-Discount-{0}", order.ID);
                    discountOrderline.ProductName = name;
                    discountOrderline.AllowOverridePrices = true;
                    discountOrderline.SetOrderLineType(OrderLine.OrderLineType.Discount);
                    discountOrderline.UnitPrice.PriceWithoutVAT = Base.ChkDouble(quantityDiscountAmount);
                    discountOrderline.UnitPrice.PriceWithVAT = Base.ChkDouble(quantityDiscountAmount);
                    discountOrderline.Price.PriceWithoutVAT = Base.ChkDouble(quantityDiscountAmount);
                    discountOrderline.Price.PriceWithVAT = Base.ChkDouble(quantityDiscountAmount);
                    discountOrderline.Save();
order.Save();

This seams to work really nice wile in cart, but this same discount becomes ignored right after it passes Checkout.

I mean are we doing something wrong with this OrderLine/order object here that it results in ignored discount line?

 

 

You must be logged in to post in the forum