Drupal Developer, Consultant, Trainer

Commerce Subscriptions

Make Drupal-based subscription systems that would make Murdoch proud!

After a lot of mental anguish, I've managed to come up with a solution for role-based subscriptions using Drupal Commerce. There are a few features floating around at the moment which claim to do this, but I think mine is (for the most part) superior.

So, what does it do?

  1. Provides a new product type called "Subscription"
  2. The subscription product type has two fields:
    • Role: Allows you to specify a role which the customer's account will be assigned after checkout has completed.
    • Role Expiry: (Optionally) allows you to specify a period, after which the assigned role will be removed; such as 1 month, 1 year etc...
  3. A rule which after checkout completion does the following:
    • Checks the order for any "subscription" products, and assigns all of the purchased roles to the order owner
    • If any of the subscription products also specify an expiry time, schedule those roles to be removed (using Rules Scheduler).

What you need to do to get this working:

  1. Ensure that the role you wish to sell currently exists. If it doesn't, go and create it.
  2. Create a new "Subscription" product
    1. Select the role you wish to sell in the Role field
    2. Optionally select a expiry time for the role in the Role Expiry field (note that you can use any valid strtotime strings if you select "Custom Expiry")
  3. Create the product display node 

It's advisable to test the configuration by adding the subscription product to your cart and completing checkout (using a test gateway or similar). Also, if you've specified an expiry time, ensure it's been added to the Rules Scheduler schedule at yoursite.com/admin/config/workflow/rules/schedule

How is this better than existing solutions?

Well, here are the alternatives that I found:

These two solutions are very inflexible, as the roles are hard-coded into the Rules actions. This new feature allows you to create multiple subscription products and have them centrally managed by the same set of rules. Much tidier.

What could be done better?

I hit a few rough patches when dealing with Rules. It is very strict about what data-types can be used to do certain things, so there is some custom code in this feature which allows our "Role Expiry" field to be used as a date data type (even though, strictly speaking, it's text). Also, there were some unsurmountable issues when trying to add and remove roles on the purchasing user that forced me to use custom PHP snippets in some of the rules actions. I'm very sorry about that, hopefully I'll work out a solution which does not require PHP module.

The module has a few dependencies:

Source code repository is located here: https://bitbucket.org/nicksantamaria/commerce_subscriptions

Attachments: