Adding Paid Content to an Existing Drupal Blog
You'll want to use this blog post in tandem with the Drush Archive dump of the Blogorama site available from the Austin 2014 DrupalCon Commerce by Example presentation page.
One of the most powerful things that you can do to a Drupal site is to add Drupal Commerce. With some modules and a bit of time, you can transform any Drupal 7 site into a revenue generation engine — no matter if you are selling physical products, file downloads, or just wanting to monetize digital content or access. The ability to simply enable commerce on an existing site is very powerful and can open up opportunities that you might not have considered.
One of those possibilities is paid content, and this post will walk you through adding paid content to an existing blog site using two modules from our Digital Commerce Suite: Commerce License (CL) and Commerce License Billing (CLB). At DrupalCon 2014 in Austin, in the second half of Commerce by Example, we walked through the process of setting up a blog. The instructions and the demo site archive are here so you can walk through at your own pace.
Goals
As noted in the session, it is important not to overthink what you are looking to accomplish. By abiding by KISS (Keep It Super Simple) you can not only focus more on your goal, but it can keep you from spending too much time initially trying to get a solution together. With that in mind, here is are the defined goals for this walkthrough:
- Create content that can only be viewed by paid subscribers.
- Provide a single page where a user can select their subscription length and then check out.
- Subscriptions will automatically renew.
How Will This Work?
Here is a quick overview of what you’ll be doing.
- Install & enable modules
- Create content & product types
- Configure Commerce License & Billing.
- Configure permissions
- Create product
- Create product display
- Misc. configuration
- Test Content & Checkout
Here’s how the basic solution works: A reader comes to your site and decides to subscribe. They click on a link in the sidebar that takes them to a page. This page includes content that you want to show the reader before they subscribe. At the bottom of the page is a dropdown allowing them to choose a monthly, quarterly, or yearly subscription. They then choose, click “Add to Cart”, and then go to the cart to complete checkout.
When they check out, an account is automatically created for them and they are instantly logged in. The product they purchased is actually a license for a role, so they are given a subscriber role that gives them instant access to all blog posts that only members of the subscriber role can see. Their plan will auto-renew based on the subscription they signed up for.
Install & Enable Modules
Commerce Core
Installing core Drupal Commerce involves installing each of these modules. These provide core functionality and are required for any Commerce solution.
- commerce - The base commerce package.
- rules - Required by commerce.
- addressfield - Also required by commerce.
Download with Drush:
drush dl commerce rules addressfield
Solution Specific Commerce Modules and Depedencies
First, you need to install the applicable modules that are needed & specific for this solution. Here are the modules (and module sets) you need to have downloaded:
- Commerce License - includes Commerce License Role - These modules to apply roles to a user when they complete a transaction.
- Commerce License Billing - Handles automatic payments and renewals of licenses.
- Entity Bundle Plugin - Required by Commerce License and allows different bundle types to have their own class.
- Commerce Card on File - Used for recurring transactions generated by Commerce License Billing.
- Commerce Immediate Login - This little helper creates an account for an anonymous user and logs them in.
- Node View Permissions - Allows permissions to be defined based on Node bundles (types). e.g. a user can see Page nodes, but not Article nodes.
These are required by Commerce License Billing:
You’ll likely already have these installed.
Download with Drush:
drush dl commerce_license commerce_license_billing entityreference entity_bundle_plugin commerce_cardonfile node_view_permissions advancedqueue date inline_entity_form views_megarow views_bulk_operations ctools views
Commerce Immediate Login in an experimental sandbox module. To learn more about sandboxes, read about them on drupal.org. To download and enable, you’ll need to execute the following commands from the document root:
cd sites/all/modules
git clone --branch master http://git.drupal.org/sandbox/simongeorges/1676742.git commerce_immediate_login
drush -y en commerce_immediate_login
cd ../../..
Enable All the Modules
Drush:
drush -y en addressfield rules commerce_cart commerce_checkout commerce commerce_ui commerce_customer commerce_customer_ui commerce_line_item commerce_line_item_ui commerce_order commerce_order_ui commerce_payment_ui commerce_price commerce_product commerce_product_pricing commerce_product_pricing_ui commerce_product_reference commerce_product_ui commerce_license commerce_license_role entityreference entity_bundle_plugin node_view_permissions commerce_payment_example views_ui views commerce_cardonfile commerce_immediate_login commerce_license_billing commerce_custom_product commerce_cardonfile_testing views_bulk_operations views_megarow date advancedqueue rules_ui rules date_api
From the Modules page:
If you need to activate the modules from the modules page, just copy this snippet into the console to get a highlighted list of what you’ll need to activate (What’s a console?):
jQuery.each(["addressfield", "rules", "commerce_cart", "commerce_checkout", "commerce", "commerce_ui", "commerce_customer", "commerce_customer_ui", "commerce_line_item", "commerce_line_item_ui", "commerce_order", "commerce_order_ui", "commerce_payment_ui", "commerce_price", "commerce_product", "commerce_product_pricing", "commerce_product_pricing_ui", "commerce_product_reference", "commerce_product_ui", "commerce_license", "commerce_license_role", "entityreference", "entity_bundle_plugin", "node_view_permissions", "commerce_payment_example", 'views_ui', 'views', 'commerce_cardonfile', 'commerce_immediate_login', 'commerce_license_billing', 'commerce_custom_product', 'commerce_cardonfile_testing', 'views_bulk_operations', 'views_megarow', 'date', 'advancedqueue', 'rules_ui', 'rules', 'date_api'], function(idx, item) { jQuery('input[name$="[' + item + '][enable]"]').parent().parent().parent().css('background', 'rgba(240, 255, 0, 0.9)'); });
Now with that out of the way, site configuration can begin.
Create Content Types & Product Type
Create Subscription Product
You need to create a product type to hold your subscription data. Navigate to Admin → Store → Products → Product types → Add product type.
Type in the name “Subscription”. Then, click Save product type.
Premium Article Content Type
The solution involves using node view permissions to limit access to paid content to subscribed users. In order to differentiate between paid and free content, a new content type needs to be created which will hold your premium content.
Navigate to Admin → Structure → Content Types → Add content type.
For this example, set the Name to “Premium Article” and the Description to “Content for paid subscribers.” Save it.
Subscription Product Display
Finally, a node will be needed to display the products that will be created later.
Navigate to Admin → Structure → Content Types → Add content type.
Set the Name to “Subscription” and the Description to “Product display for subscription products.” Save it and add fields. Now, you’ll want to add a Product Reference field with any title you want. When you’re editing the field on the next page, make sure you make the field required, set the number of values to unlimited, and set the type of referenced products to Subscriptions.
Add a Subscriber Role
Go to Admin → People → Permissions → Roles and create a new role called “subscriber”. This will be the role that people will receive.
Configure Commerce License & Billing
Now that the content & product types have been created, you need to set up Commerce License & Commerce License Billing. The first order of business is to create the billing cycle types, and then add the license information to the product created in the previous step, followed by configuring the licensed role.
Configure the Billing Cycle Types
In order to provide the various billing options, you need to create the types of cycles desired. To do this, head over to Admin → Store → Configuration → Billing Cycle Types and then click on “Add billing cycle license type”. What you’ll want to do is create one of these for each of the billing cycle length’s you’ll be offering. Create one for each of Monthly, Quarterly, and Yearly. Make sure Asynchronous is checked for each one.
Once that’s completed, you need to add the License information to your product types.
Configure Commerce License
Commerce License needs to know what product and line item types you’ll be associating with licenses. To configure this, navigate to Admin → Store → Configuration → License Settings. Now check the checkboxes next to the Product Line item and the Subscription product type you created earlier. Save Configuration.
Now, click on the Role tab. Click the checkbox next to Subsription and Save.
If you go look at the field configuration for the Subscription product, you’ll now see several additional fields that License has automatically added.
Next, some permissions need to be configured.
Configure Permissions
Setting Up Node View Permissions
First, Node View Permissions needs to know which content types you’re wanting to manage with specific permissions. Navigate to Admin → Configuration → Content Authoring → Node view permissions. Select the checkboxes next to Article and Premium Article. Save configuration.
Role Permissions
Next, navigate to Admin → People → Permissions. You’ll want to activate the following permissions.
- Make sure all roles have access checkout (to enable anonymous checkout).
- Authenticated User: View / Edit / Delete / Create own card data.
- Authenticated User: view own orders of any type.
- Anonymous User: Article: View any content.
- Subscriber: Premium Article: View any content.
Create the Products
Ultimately, we’d like to give people three options for subscribing: monthly, quarterly, and yearly subscriptions. To do this we’ll need to create three products. Go to Store → Products → Add a product → Create Subscription. Here, you’ll create three products. The SKU and Title don’t matter too much, but for this exercise, just put in “SUB-MONTHLY”, “SUB-QUARTERLY”, or “SUB-YEARLY” for each of the respective products. Do something similar for the titles: “Monthly Subscription”, “Quarterly Subscription”, and “Yearly Subscription”. For prices, go ahead and put in $9.99, $59.99, and 99.99 respectively.
Underneath the price field, you’ll see a few more additional fields. For the role, select the role you created above. For License duration, choose “Unlimited”. When you get to License billing cycle type, choose the appropriate value for the product you are creating. For License billing type, select “Prepaid”.
Create Product Display
Okay, now that we’ve gotten Commerce set up, it’s time to set up the product display bundle. It’s responsible for creating the page that your visitors will see when they are going to subscribe to the blog.
- Go to Content → Add Content → Subscription.
- For the title: “Subscribe Today!”
- For the body: Your lipsum of choice.
- Under the products, click the checkboxes next to the SKUs for the 3 products you created earlier.
- For the demo, add a menu link called “Subscribe” on the main menu.
- Save it.
Miscellaneous Configuration
- If you added the Commerce Immediate Login module, it should have automatically modified an existing rule (Create a new account for an anonymous order) to instantly log in a newly created user after the completion of checkout. If it isn’t working, you might need to check to see if the final action of the rule is “Log in user” and if it isn’t, add that action and set the Data selector to “account-created”.
- Feel free to clean up the display of the Product display by doing the following:
- Under Display Settings for the Subscription content type, make sure that “Display author and date information.” is not checked.
- Under Comment Settings, make sure that “Default comment setting for new content” is closed.
- You will have to update your existing node to make the comments setting take effect.
- Navigate to Manage Display for the Subscription Content type. Here, you'll notice that there are messages saying you need to go to the manage display for the product type. The reason for this is that Commerce gives the product types control over what is actually displayed on the product node page. Click on any of the links which take you to the manage display for the subscription product type (admin/commerce/products/types/product/display).
- Ensure that “License type”, “SKU”, “License duration”, and “Role” are all hidden. Save it.
Card on File & Payments
For the demo we’re using the default payment options that come with commerce, but feel free to use any payment gateway that supports Card on File.
Create Test Blog Post & Checkout!
To test, just create a new Premium Article. You can either log out, open another browser, or create a private browsing session to view the main page and notice that the content won’t show up. Add a subscription to the cart, complete the checkout process, navigate over to the home page, and your content will be visible!
Comments
what hapens next, after subscription has ended?
Very nice post and I am trying to make it work on my website.
I just wonder what happens after the end of the subscription.
Can it be reactivated?
git command not working in drush for Windows
Excellent post on using Drupal Commerce to limit access to website content. I really enjoyed reading your article. I did have one problem so far. When I attempt to use the "git" command you provided in my drush command prompt for Windows (to ), I am told that: " 'git' is not recognized as an internal or external command, operable program or batch file." Not sure why that is. Any thoughts? I apologize ahead of time if I am not seeing the obvious as I am fairly new to using drush.
Order list on new created user
Hello - first - tks for this tutorial - GREAT.
I just did a test checkout and bought a montly sub. When viewing the order as the new user - I see 2 orders. I have deleted the account and did it all over - and again -> 2 orders. One pending. Why is that? Did I miss someting?
Tks again