Generating the subscriptions
How to trigger the subscription generation based on the source order and the subscription model configuration
Problem
You have associated a subscription model with your market, setting the allowed subscription frequencies and choosing the subscription strategy. You also have selected a source order containing some line items with frequency from which to start generating the subscriptions and placed it. You now want to trigger the subscription generation.
Solution
To start generating the subscription from a placed source order, send a PATCH
request to the /api/orders/:id
endpoint setting the _create_subscriptions
attribute to true
.
Make sure to start the subscription generation upon/after the order placement, otherwise no order subscription will be generated even if the source order contains some line items with frequency.
Example
The following request generates the subscriptions from the source order identified by the "NgojhelVGm" ID:
Additional notes
How automatic subscription generation works
The automatic order subscription generation process is triggered manually by using a specific attribute. The necessary order subscriptions are automatically generated based on the source order’s line item for which you’ve specified a frequency, accordingly to the strategy set at the subscription model level.
The source order’s customer email is stored, that order is considered the subscription's first run, and the generated order subscriptions are activated (unless differently specified at the subscription model level by setting the
auto_activate
attribute totrue
).For each automatically generated order subscription, some source order information is copied (e.g. addresses, customer payment source, etc.), the related order subscription items are created and associated with the order subscriptions, and the target order is placed at the scheduled time using the saved customer payment source (if still valid).
During an automatic order subscription generation, the source order is never reused to generate any next runs, which are built on top of the associated order subscription and related order subscription items only.
Which and how many order subscriptions and order subscription items are generated depends on the subscription strategy and source order's line items.
Order subscriptions
by_frequency
— as many order subscriptions as the different frequencies specified for the source order’s line items are generated, each with the related order subscription items (default strategy).by_line_items
— an order subscription is generated for any source order’s line item that has a frequency, each with the related order subscription item.
Order subscription items
by_frequency
— for each automatically generated order subscription as many order subscription items as the source order’s line items that have that specific frequency are generated.by_line_items
— for each automatically generated order subscription a single order subscription item is generated.
Example
Let's look at the source order we used throughout this guide:
The following request fetches the source order, requesting some basic fields and including the line items to check their frequency:
As you can see inspecting the above response it contains 4 line items of type skus
for a total of 5 SKUs:
2 SKUs with
monthly
frequency (both with quantity1
— SKU codesLENSPACKL125
andLENSPACKR075
)1 SKU with
weekly
frequency (with quantity2
and SKU codeRAZRFILLPACK4
)1 SKU with no specified frequency (with quantity
1
and SKU codeMUGXXXAUFFFFFF00000011OZ
)
Based on the subscription strategy these are the subscriptions and subscription items that will be generated (remember that line items without frequency don't generate subscriptions):
The generated recurring target orders cannot be used as source orders from which to trigger a new subscription generation.
You can check the generated order subscriptions and related order subscription items in the Subscriptions section of our admin dashboard, or via API by sending a GET
request to the /api/order_subscriptions
endpoint, filtered by the source order ID with order subscription items included (the following request is related to the by_frequency
example above):
The following request fetches all the order subscriptions associated with the customer identified by the "nDNahABVZR" ID and generated by the source order identified by the "NgojhelVGm" ID, including the related order subscription items:
Managing failed subscriptions
You can track subscriptions' renewal failures by activating a webhook listening for the order_subscriptions.last_run_failed
event and acting accordingly (e.g. setting up an automatic notification system for the customer).
The order_subscriptions.last_run_succeeded
event is available as well.
Notifying upcoming subscriptions
You can notify customers about upcoming recurring orders associated with subscriptions by properly setting the renewal_alert_period
attribute at the order subscription level and leveraging the order_subscriptions.renewal
webhook event that will be fired accordingly.
The renewal alert period must be expressed in hours (e.g. if you want to send the notification 3 days before, set it to 72
). The minimum value is 1.
More to read
Last updated