Manually adding order to 4.20 - how to generate order number?

4 months ago
Upgrading a plugin I wrote for v3.70.    I'm using OrderService.InsertOrder() to manually add orders.   This worked fine before but now generates exception that CustomOrderNumber cannot be null.

Tried an empty string for CustomOrderNumber - that got rid of the exception but all my added orders now show empty order numbers.

Thought it might need a mask so I tried "{ID}", but (you guessed it) all my added orders then had an order number of "{ID}"

So what do I put in CustomOrderNumber when using InsertOrder()?   I'd like nopCommerce to just use it's default method of generating order numbers for new orders (be that ID, or a mask or whatever is configured)
4 months ago
look into the SaveOrderDetails method in Nop.Services/Orders/OrderProcessingService.cs, which taps into the CustomNumberFormatter.cs service:

  //generate and set custom order number
order.CustomOrderNumber = _customNumberFormatter.GenerateOrderCustomNumber(order);
_orderService.UpdateOrder(order);


which then checks against your order settings for a custom mask to re-use the order.Id, or generates something based on datetime
4 months ago
That's the ticket!   Thank you.
4 months ago
Followup - to help future others doing what I'm doing ....

In a multi-store setup GenerateOrderCustomNumber will use the default store settings with regard to what custom order mask to use.  I needed it to use a specified store's mask.   So what I did is:

_orderService.InsertOrder(ordNop);
var cnf = new CustomNumberFormatter(_settingService.LoadSetting<OrderSettings>(iStoreId));
ordNop.CustomOrderNumber = cnf.GenerateOrderCustomNumber(ordNop);
_orderService.UpdateOrder(ordNop);

I can confirm this works.  So unless you see a bunch of replies below this message saying "NO! NO! NO! NO!", you can assume that this is the right way to do it. <wink>