How to create subscription/repeat orders in a background service (no session) – Umbraco Commerce 13

Hi all,

I’m working on a project using Umbraco Commerce 13 where we need to support subscriptions/repeat orders.

We have a background service (no HTTP session) that should create new orders on a schedule (e.g. weekly/monthly).

When I try to use the usual APIs (like GetOrCreateCurrentOrder), they return null because they rely on session.

I also couldn’t find CloneOrder in my IOrderService — it seems that was introduced in later versions?
I tried:

var newOrder = _orderService.CreateOrder(storeId, currencyId, customerReference);

…but in Umbraco Commerce this overload doesn’t exist.

Any suggestions.

Thanks

All domain models have factory methods so to create an order you’d use Order.Create(...);. This needs to be called within a unit of work. Once you’ve created your order you can persist it in the usual way _orderService.SaveOrder(order); making sure to complete your uow.

Thanks for you suggestion. It worked for me .

The next issue I’ve hit is with AddProduct(...). When I call it in my background service, it tries to resolve a product snapshot internally via IProductService, which blows up because there’s no published snapshot available (no HTTP request context). The exception is:

System.InvalidOperationException: Wasn't possible to get a valid Snapshot
   at Umbraco.Extensions.PublishedSnapshotAccessorExtensions.GetRequiredPublishedSnapshot(...)

Since my subscription processor runs outside of a request pipeline, there’s no PublishedSnapshot, so I can’t rely on the default AddProduct overload.

Please suggest.

Thanks

Can you provide a full stack trace as I’m not entirely sure where that is being called from