Here is a decompiled version of the method (please be advised, that original variable names might have gotten lost in the compilation process):
// Dynamicweb.eCommerce.Orders.Order
public void DowngradeToCart()
{
string id = this.ID;
string number = NumberGenerator.GetNumber("CART");
Database.ExecuteNonQuery(string.Format("UPDATE EcomOrders SET OrderID = '{0}', OrderCart = {1} WHERE OrderID = '{2}'", number, Database.SqlBool(true), id), "Ecom.mdb");
this.ID = number;
this.IsCart = true;
Database.ExecuteNonQuery(string.Format("UPDATE EcomOrderLines SET OrderLineOrderID = '{0}' WHERE OrderLineOrderID = '{1}'", number, id), "Ecom.mdb");
this._OrderLines = null;
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("OrderID", id);
dictionary.Add("OrderCart", false);
Database.CopyRow(string.Format("SELECT * FROM EcomOrders WHERE OrderID = '{0}'", number), dictionary);
foreach (OrderLine current in this.OrderLines)
{
Dictionary<string, object> dictionary2 = new Dictionary<string, object>();
dictionary2.Add("OrderLineID", NumberGenerator.GetNumber("OL"));
dictionary2.Add("OrderLineOrderID", id);
Database.CopyRow(string.Format("SELECT * FROM EcomOrderLines WHERE OrderLineID = '{0}'", current.ID), dictionary2);
}
}
What the method does: The method apparently changes the id of the order to a cart id and all of its order line order ids to point to the new cart id and sets the order's OrderCart field in the DB to true (or 1). Afterwards, it copies the current order and all its order lines to the old (and now unoccupied) order id and sets the copied order's OrderCart field in the DB to false (or 0).
The problem: However, as you can probably see, the OrderLineParentLineID field in the DB is not being updated when downgrading the order to a cart, which means that any copies of child order lines (usually all product discount order lines) will still point to the parent order lines from the copied order.
My suggestion: I suggest changing the method, so it will first generate the sequential ID numbers for each copied order line before saving any of the copies. Then it should save each order line by taking care to save the parent order lines before their child order lines, and also take care update the parent line id field to the new id of its parent order line. And please, do this whole process in a database transaction (and hopefully using prepared statements instead) (if your API can let you do that).
Thank you for reading.
Alex