Hi, I would like to insert a custom “negative orderRow” into the cart based on session-information/custom logic But can’t figure out the best/right way of doing it.
Could someone give me some pointers of the best way/api-calls of doing it?
We need to allow users to pay part of the amount using cash or points. And the rest using Litium so we need to be able to add custom logic to reduce the price by adding a row for split payment.
How do you recognize a given customer has paid in Cash or has points to be used as cash?
Is the customer logged in, so you can check somehow? or do the customer has some kind of a voucher code that customer enters to claim his payment?
The order rows need to be connected to the products, and a recalculation may potentially remove them automatically. Best would be to add it as a order level discount. I will come back to you with code.
Here is the sample code. You will need to test for negative values also (if discount given is larger than current order total.)
private void AddOrderLevelDiscount()
{
//Sample code is put in the CheckoutB2Controller, and called from HTTP GET method overload of public ActionResult Index(CheckOutB2C currentPageDefinition)
//Should not modify the order if the order has already being placed, discount added only to Init orders.
if (CurrentOrderCarrier.OrderStatus == (short)OrderState.Init)
{
//the discount given is on current order currency.
var discountAmount = 100;
var discountAmountWithVat = 125;
var vatAmount = discountAmountWithVat - discountAmount;
var vatPercentage = decimal.Divide(vatAmount, discountAmount);
var discountCarrier = CurrentOrderCarrier.OrderDiscounts.FirstOrDefault(x => x.CampaignID == Guid.Empty); //Identify the discount based on not related to a campaign.
//IF you need multiple discounts this way, you would need to either add them to a single discount, or use the description to identify it.
//Using the discount Guid ID is not reliable as it may get re-generated.
if (discountCarrier == null || discountCarrier.CarrierState.IsMarkedForDeleting)
{
//add as a new discount.
discountCarrier = new Foundation.Modules.ECommerce.Carriers.OrderDiscountCarrier()
{
ID = Guid.NewGuid(),
DiscountAmount = discountAmount,
DiscountDescription = "Custom discount of ",
DiscountPercentage = 0,
OrderID = CurrentOrderCarrier.ID,
VATAmount = vatAmount,
VATPercentage = vatPercentage,
DiscountAmountWithVAT = discountAmountWithVat,
CampaignID = Guid.Empty, //Do not set this ID as Litium would assume this as a "Campaign" and campaign calculator will try to re-calculate, resulting in this row getting removed.
ExternalCampaignID = null //Do not set this ID as Litium would assume this as a "Campaign" and campaign calculator will try to re-calculate, resulting in this row getting removed.
};
CurrentOrderCarrier.OrderDiscounts.Add(discountCarrier);
}
else
{
//TODO: if the amount need to be updated, adjust here.
}
//recalculate the order since the amounts are now changed.
_moduleECommerce.Orders.CalculateOrderTotals(CurrentOrderCarrier, _securityToken);
_moduleECommerce.Orders.CalculatePaymentInfoAmounts(CurrentOrderCarrier, _securityToken);
}
}
Note that, as the order total changes, there would be order total calculations.
You can override the OrderTotalCalculation and adjust the discount as well.
please see below.