Updating the subscriptions

How to customize the next subscription occurrences

Problem

You have generated one or more subscriptions from a source order, based on the subscription model configuration. At some point, you may want to suspend some of them, re-activate them, or even change the next subscription occurrences in terms of frequency, items, expiration date, and more.

Solution

Once generated, automatic order subscriptions live a life of their own, meaning that any changes to the associated subscription model or source order won't affect the already generated order subscriptions and will eventually apply starting from the next subscription generation only.

To update an already generated subscription you need to edit the order subscription itself or the related order subscription items. You can do it any time in between each run.

Updating the order subscription

Suspending, activating, and cancelling an order subscription

Active order subscriptions can be deactivated (temporarily suspended) and eventually re-activated, or cancelled.

Deactivate

To deactivate an order subscription, send a PATCH request to the /api/order_subscriptions/:id setting the _deactivate trigger attribute to true:

The following request deactivates the subscription identified by the "BkMdJFGdjX" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscriptions/BkMdJFGdjX' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscriptions",
      "id": "BkMdJFGdjX",
      "attributes": {
        "_deactivate": true
      }
    }
  }'

Activate / Reactivate

To activate an order subscription, send a PATCH request to the /api/order_subscriptions/:id setting the _activate trigger attribute to true:

The following request (re)activates the subscription identified by the "BkMdJFGdjX" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscriptions/BkMdJFGdjX' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscriptions",
      "id": "BkMdJFGdjX",
      "attributes": {
        "_activate": true
      }
    }
  }'

Cancel

To cancel an order subscription, send a PATCH request to the /api/order_subscriptions/:id setting the _cancel trigger attribute to true:

The following request cancels the subscription identified by the "mPbnqFvRPN" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscriptions/mPbnqFvRPN' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscriptions",
      "id": "mPbnqFvRPN",
      "attributes": {
        "_cancel": true
      }
    }
  }'

Changing the subscription frequency

To update the frequency of an order subscription, send a PATCH request to the /api/order_subscriptions/:id setting the frequency attribute:

The following request changes the frequency of the subscription identified by the "BkMdJFGdjX" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscriptions/BkMdJFGdjX' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscriptions",
      "id": "BkMdJFGdjX",
      "attributes": {
        "frequency": "two-month",
        "next_run_at": "2023-05-23T17:30:00.000Z"
      }
    }
  }'

Changing the subscription expiration date

Adding an expiration date to an order subscription is optional. To set or change the expiration date of an order subscription, send a PATCH request to the /api/order_subscriptions/:id setting the expires_at attribute:

The following request changes the expiration date of the subscription identified by the "BkMdJFGdjX" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscriptions/BkMdJFGdjX' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscriptions",
      "id": "BkMdJFGdjX",
      "attributes": {
        "expires_at": "2023-12-31T23:59:59.000Z"
      }
    }
  }'

Changing the subscription next run date

To update an order subscription's next occurrence date, send a PATCH request to the /api/order_subscriptions/:id setting the next_run_at attribute:

The following request changes the next occurrence date of the subscription identified by the "BkMdJFGdjX" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscriptions/BkMdJFGdjX' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscriptions",
      "id": "BkMdJFGdjX",
      "attributes": {
        "next_run_at": "2023-04-07T14:30:00.000Z"
      }
    }
  }'

Changing the customer payment source

The recurring orders associated to a subscription are placed using the saved customer payment source (if still valid). If you need or want to update it, send a PATCH request to the /api/order_subscriptions/:id setting the customer_payment_source relationship:

The following request associates the customer payment source identified by the "OvEOysMVAD" ID with the order subscription identified by the "BkMdJFGdjX" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscriptions/BkMdJFGdjX' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscriptions",
      "id": "BkMdJFGdjX",
      "relationships": {
      "customer_payment_source": {
        "data": {
          "type": "customer_payment_sources",
          "id": "OvEOysMVAD"
        }
      }
    }
    }
  }'

Updating the order subscription items

To avoid any possible issue or date inconsistency (especially in the case of high-frequency subscriptions) we suggest deactivating the order subscription before making any changes to the associated order subscription items and reactivating it right after.

Changing the quantity of a product belonging to a subscription

To change the quantity of a product belonging to a subscription, send a PATCH request to the /api/order_subscriptions_item/:id setting the quantity attribute:

The following request updates the quantity of the order subscription items identified by the "adkLKwFVzG" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscription_items/adkLKwFVzG' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscription_items",
      "id": "adkLKwFVzG",
      "attributes": {
        "quantity": 3
      }
    }
  }'

Changing the price of a product belonging to a subscription

To change the unit price of a product belonging to a subscription, send a PATCH request to the /api/order_subscriptions_item/:id setting the unit_amount_cents attribute:

The following request updates the unit price of the order subscription items identified by the "adkLKwFVzG" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscription_items/adkLKwFVzG' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscription_items",
      "id": "adkLKwFVzG",
      "attributes": {
        "unit_amount_cents": 1790
      }
    }
  }'

Changing one of the subscription products

To change one of the products belonging to a subscription, send a PATCH request to the /api/order_subscriptions_item/:id setting the sku_code or bundle_code attribute:

The following request changes the SKU code of the order subscription items identified by the "adkLKwFVzG" ID:

curl -g -X PATCH \
  'https://yourdomain.commercelayer.io/api/order_subscription_items/adkLKwFVzG' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscription_items",
      "id": "adkLKwFVzG",
      "attributes": {
        "sku_code": "TOOTHPASTEMINT150G",
        "unit_amount_cents": null
      }
    }
  }'

Adding a new product to a subscription

To manually add a new product to an already generated subscription you just need to create a new order subscription item. To do that, send a POST request to the /api/subscription_items endpoint specifying the quantity attribute and setting the order_subscription and item relationships:

The following request creates a new order subscription item from the item identified by the "nJoqSzPEAq" ID and associates it with the order subscription identified by the "DPAwmFnYkJ" ID:

curl -g -X POST \
  'https://yourdomain.commercelayer.io/api/order_subscription_items' \
  -H 'Authorization: Bearer your-access-token' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
    "data": {
      "type": "order_subscription_items",
      "attributes": {
        "quantity": 1
      },
      "relationships": {
        "order_subscription": {
          "data": {
            "type": "order_subscriptions",
            "id": "DPAwmFnYkJ"
          }
        },
        "item": {
          "data": {
            "type": "skus",
            "id": "nJoqSzPEAq"
          }
        }
      }
    }
  }'

Removing a product from a subscription

To manually remove a product from an already generated subscription you just need to delete the related order subscription item. To do that, send a DELETE request /api/subscription_items/:id endpoint:

The following request deletes the order subscription item identified by the "ydzZAyFpNb" ID:

curl -g -X DELETE \
  'https://yourdomain.commercelayer.io/api/order_subscription_items/ydzZAyFpNb' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer your-access-token'

More to read

Last updated