In my example in order to generate the shipping i needed the ShoppingCart object, which if i get from the Order on the 1st time, it wasn't accurate, so recursion didn't help.
But i did find out the correct way to go about this. Even if an order is already created, the ShoppingCartInfoProvider.SetOrderInternal(ShoppingCartInfo cartObj, bool generateInvoice) is called 1 time. This method will then cause the OrderItem.Update to trigger a bunch.
So i overwrote that method, used the cartObj (which is the TRUE order status) to find the ShippingTax and Discount, and set it in the cartObj.ShoppingCartCustomData.
Then i used the OrderItem.Update.Before with a recursion to catch the first execution and GET The OrderObject's CustomData values of my taxes and set the corresponding fields.
[assembly: RegisterCustomProvider(typeof(CustomShoppingCartInfoProvider))]
public class CustomShoppingCartInfoProvider : ShoppingCartInfoProvider
{
protected override void SetOrderInternal(ShoppingCartInfo cartObj, bool generateInvoice)
{
try {
// Set some Custom Order Data so the OrderInfo.Events.Insert.Before and OrderInfo.Events.Update.Before can latch onto them.
double ShippingCost = ShippingOptionInfoProvider.CalculateShipping(cartObj);
cartObj.ShoppingCartCustomData.SetValue("ShippingTax", ShippingOptionInfoProvider.CalculateShippingTax(cartObj));
cartObj.ShoppingCartCustomData.SetValue("ShippingDiscount", ShippingOptionInfoProvider.CalculateShippingDiscount(cartObj, ShippingCost));
} catch(Exception ex)
{
EventLogProvider.LogException("CustomShoppingCartInfoProvider", "ERRORSETTINGSHIPPINGVALUES", ex, cartObj.ShoppingCartSiteID, additionalMessage: "Could not set the Shipping Tax and Shipping Discount for the order due to an error calculating shipping.");
}
base.SetOrderInternal(cartObj, generateInvoice);
}
}
.
protected override void OnInit()
{
// Add Custom Fields from Order Custom Field webpart to Order from Shopping Cart and handle getting the Shipping Taxes and Discount from the Custom Data (Set in the Custom Shopping Cart Info Provider)
OrderInfo.TYPEINFO.Events.Insert.Before += OrderInsert_Before;
OrderInfo.TYPEINFO.Events.Update.Before += OrderUpdate_Before;
}
private void OrderUpdate_Before(object sender, ObjectEventArgs e)
{
try
{
OrderInfo OrderObject = (OrderInfo)e.Object;
RecursionControl FirstTimeSetTaxAndDiscount = new RecursionControl("OrderUpdate_" + OrderObject.OrderID);
if(FirstTimeSetTaxAndDiscount.Continue) {
// Set shipping tax and discount fields set from the CustomShoppingCartInfoProvider.SetOrderInternal
if (OrderObject.OrderCustomData.ContainsColumn("ShippingTax"))
{
OrderObject.SetValue("OrderTotalShippingTaxesInMainCurrency", Convert.ToDouble(OrderObject.OrderCustomData.GetValue("ShippingTax")));
}
if (OrderObject.OrderCustomData.ContainsColumn("ShippingDiscount"))
{
OrderObject.SetValue("OrderTotalShippingDiscountInMainCurrency", Convert.ToDouble(OrderObject.OrderCustomData.GetValue("ShippingDiscount")));
}
}
} catch(Exception ex)
{
EventLogProvider.LogException("CustomLoader", "OrderUpdate_Before", ex);
}
}