Developer forum

Forum » Development » Change Order.ID

Change Order.ID


Reply
Hi,

I'm trying to manipulate the Order object from the CheckoutDoneOrderIsComplete subscriber.
I can change the user data easily but when I change the Order.ID a duplicate of the order is made.

How can this be done?

The code looks like this:

    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Ecom7.Cart.Notifications.CheckoutDoneOrderIsComplete)]
public class CheckoutDone: Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            var myArgs = (Dynamicweb.Ecom7.Cart.Notifications.CheckoutDoneOrderIsCompleteArgs)args;
            myArgs.Order.ID = "JK" + myArgs.Order.ID;
            myArgs.Order.CustomerComment = "Changed by JK";
            myArgs.Order.Save();
           
        }
    }

Best regards.

Replies

 
Reply

Hi Jonas

You can override the OrderId "logic" by updating the order id in the database.
Run this code before saving the order.

private void UpdateOrderID(string oldId, string newId) {
 string SQL = "";

 SQL = "UPDATE EcomOrders SET OrderID = '" + newID + "', OrderCart = " + Database.SqlBool(false) + " WHERE OrderID = '" + oldID + "' ";
 Database.ExecuteNonQuery(SQL, "Ecom.mdb");

 SQL = "UPDATE EcomOrderLines SET OrderLineOrderID = '" + newID + "' WHERE OrderLineOrderID = '" + oldID + "' ";
 Database.ExecuteNonQuery(SQL, "Ecom.mdb");

}


/Thomas

 
Reply
Thanks a lot, Thomas !
 
Reply
Hi Thomas,

Are you aware of any side effects of this solution? Can I just change the Order ID like that without running into problems later? Are Order and OrderLines the only two tables with a reference to the Order ID?

Imar
 
Reply
In addition to my earlier question (which is still unanswered), is there a safe location to update the order earlier than in the Completed step? I need to pass localized order IDs to a payment gateway, so I need to update the ID before I send it of the gateway.

Any ideas?

Imar
 
Nicolai Høeg Pedersen
Reply
In cartv2 - you have Notifications.OrderIsPassedToCheckoutHandler which is fired just after cart is converted to order and just before the checkout handler takes over. In there you can change the order ID.
This notificiation passed an order instance - set the ID on that and save - it is the same instance that is rendered in the checkout handler.

In cartv1 Notifications.eCommerce.Order.Steps.Confimed is fired just before the order is converted to order and then rendered... Making it difficult to change the order ID...

So - in a OrderTemplateExtender which is called every time an order is rendered, including when rendering for the gateway, there is a state, TemplateExtenderRenderingState.Before and TemplateExtenderRenderingState.After.

In the state before, when order.iscart = true and order.step = 5 is just before the order is rendered for the gateway. In that specific situation it should be possible to change the orderid.

In the extender:
if TemplateExtenderRenderingState.Before and order.iscart=true and order.step=5 then
-Change the ordernumber
end if
 
Reply
Hi Nicolai,

It seems that Render on the Gateway provider is called before the OrderTemplateExtender. Since I need the updated ID in Render, my update code fires too late. Am I doing something wrong?

Could I also update the order ID in Render on the Gateway? Or are changes not picked up there?

Imar
   
 
Nicolai Høeg Pedersen
Reply
If you have your own gateway provider you have OnBeforeRenderStep5 event and the possibility to override Render method. And in there you can change the order id.
 
Reply
Yes, I have my own gateway so I can override Render or OnBeforeRenderStep5. Is one preferred over the other?

Sorry for asking so many questions; I believe I am getting close now, and really appreciate the help I am getting. I have a few other questions regarding gateways and the different status fields that are available which I'll post in a separate thread.

Thanks,

Imar
 
Reply
OnBeforeRenderStep5 seems to do the trick. Thanks.

I also noticed that Render is being called in Step3 as well. At that stage, I am not really interested in seting template tags such as the Order.Gateway.Merchant. Is it simply a matter of checking Dynamicweb.eCommerce.Context.Cart.StepNum and only render in step 5?

Imar
 
Nicolai Høeg Pedersen
Reply
OK with the questions - I also get to learn stuff :).

In cartv1 gateway is rendered also in step 3 because that is where payments are rendered. So just check for step 5.
 
Reply
@Imar, comment #4: 

As to key references: Yes, you can change the order just like that, and no key references in other places will become orphants.

Regards /Morten
 
Reply
Hi Morten,

Thank you. It seems to work fine so far.

Enjoy your weekend.

Imar
 
Gaëtan Di Caro
Reply

Sorry for bumping a 7 year-old thread, but i need the same feature now in version 9, i.e our customer wants a custom way of naming the orderid that isn't covered by the auto-numbering.

I tried the notification OrderIsPassedToCheckoutHandler but it doesn't work : it seems like the order is duplicated in the database, one line with the old id, and one line with the new ID. I tried a couple of other notifications, but I haven't found anything that works the way I want.

Here's the code :

[Dynamicweb.Extensibility.Notifications.Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.OrderIsPassedToCheckoutHandler)]
    public class OrderNumbering : NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
        {
            var myArgs = (Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.CheckoutDoneArgs)args;
            var oldID = myArgs.Order.Id;
            var newID = "ORDER" + oldID.PadLeft(8, '0');

            myArgs.Order.Id = newID;

            string SQL = "";

            SQL = "UPDATE EcomOrders SET OrderID = '" + newID + "', OrderCart = " + Database.SqlBool(false) + " WHERE OrderID = '" + oldID + "' ";
            Database.ExecuteNonQuery(SQL);

            SQL = "UPDATE EcomOrderLines SET OrderLineOrderID = '" + newID + "' WHERE OrderLineOrderID = '" + oldID + "' ";
            Database.ExecuteNonQuery(SQL);
        }
    }

 
Nicolai Pedersen
Reply

Well, you need to make sure you save the order and replaces the one in the context as well. I cannot tell you exactly how.

I will not recommend this as it is messing with the order in a critical step. Also warn that doing this could cause issues later on if flow changes just a little bit.

 
Anders Ebdrup
Anders Ebdrup
Reply

Hi Nicolai,

 

In the latest version af Dynamicweb (9.4) is it now "more" possible to overwrite the orderid so we can have different orderids for different shops?

 

Best regards, Anders

 
Nicolai Pedersen
Reply

Hi Anders

Maybe... We had something that prevented orders from having orderids not starting with "ORDER". Now you can use whatever.

But maybe ellaborate your question? To be sure I understand...

BR Nicolai

 
Anders Ebdrup
Anders Ebdrup
Reply

Hi Nicolai,

 

We have a requirement for having different number series used for two different shops in the same Dynamicweb solution and we are trying to figure out how to solve that. And one way is to overwrite of the orderid by some custom code.

 

Best regards, Anders

 
Gaëtan Di Caro
Reply

It would be interesting to provide us with hooks to the orderid transformation event, or allow "OrderIDTransformationProvider" (for example) so we can override with custom code.

 

Customers with heavy ERP integration sometimes have quite strict naming rules.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi guys,

We had this come up again recently in a multi-language site where the English site uses Order and then the country sites would like to use a translated version.

Would it be an option to give us something like a BeforeOrderId subscriber that allows us to define our own prefix based on whatever logic is required (such as the current site) and then would fall back to pulling it from the order number settings when none is supplied? To me that feels like a cleaner approach than updating rows in the database directly.

Imar

 
Nicolai Pedersen
Reply
This post has been marked as an answer

I've added a BeforeUpdateCartToOrder notification that is raised when the convert happens - you get the order object and the newly created orderid that you can then override.

TFS#55221, due with next 9.5 hotfix (Dynamicweb.Ecommerce package)

Votes for this answer: 3
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Wonderful. Thank you very much!

 
Gaëtan Di Caro
Reply

Nice !

 
Anders Ebdrup
Anders Ebdrup
Reply

very much appreciated! :-)

 

You must be logged in to post in the forum