S1L

Selling access to Organic Groups with Drupal Commerce

By Sil, 03/16/2013 - 15:24
Selling Organic Groups access with Drupal Commerce and Rules

UPDATE 30 Oct. 2014:

Instead of using the 'Rules + Organic groups' method describe in this blogpost, I advise you to have o look at selling access to Organic Groups with Commerce Licenses. It's easier to implement, less intricate to maintain and more future-proof for when your Drupal website evolves in the future.

I created step by step documentation for Commerce License OG at https://www.drupal.org/node/2366023

cheers,

Sil

------

There are several ways to sell access to content on your Drupal site. One of the ways that hasn't been discussed much in Drupal tutorials is selling access to private Organic Groups with Drupal Commerce. 

As I needed this for my own project I hired Hibersh to create the Rule configurations for me. It worked perfectly ever since. As I saw other people struggling with achieving the same thing I decided to spend half a day writing this tutorial for you. I hope it helps you doing Drupal :)  

What you get

First let me discuss what you will get at the end of this tutorial:

Drupal users can purchase access to an Organic Group by purchasing a certain Drupal Commerce product.

You are going to make it so that you won't have to create a custom Rule for every Organic Group. You will have a 'universal' Rule configuration. Your site administrators or content admins can manage products and Organic Groups and the product -> Organic Group relationship and add and delete them as they see fit - if you decide to allow them to do that if course :)

Neat fact: buying 1 product can give access to 1 or several Organic Groups if you like.

To Make it Work

I've actually made 2 tutorials for you:

the Quick version: enable the example Feature and add a few nodes and you're done

the Drupalist version: learn how the structure works - knowing the right Fields and Rules configuration enables you to make it work for your own usecase. You will feel you've mastered a cool Drupal skill once you completely understand how this works!

the Quick version

Enable these modules: Organic Groups, Organic Groups access control (and Organic Groups UI). They are all part of the og project on drupal.org. Other dependencies Strongarm and Inline Entity Form. That last one is because I've built the Feature in Commerce Kickstart 2.x. Don't forget to enable Features module.

Download the selling_content feature as a zip file or through Git.

Enable the selling_content Feature on your site.  

Create 1 'Content Group' node,

Create 1 'Group Content' node and make it reference your 'Content Group' node.

Create 1 'Product for Organic Group' and make it reference your 'Content Group' node.

Done.

the 'Drupalist version'

Now let's have a look what we've actually built by enabling that selling_content Feature.

The Feature consists of a structure and a Rules configuration.

The structure

structure for selling access to Organic Groups

The Product Display -> Product reference

This is how most Drupal Commerce stores are configured. If you are not familiar with them, learn about them in this tutorial about Product Displays.

The Product -> Organic Group reference

This allows you to create a Drupal Commerce product and reference the Organic Groups it should give access to. Our Rules configuration below allows us to reference multiple Organic Groups from one product - or just one if your usecase needs to give people access to one Organic Group per product purchase. The Organic Groups need to be private and the content copies this behavior.

The Content -> Organic Group relationship

This is how most Organic Groups are structured: A content node with an Organic Groups field that makes it behave as 'Group content' and a group node with an Organic Groups field that makes it behave as 'Group'. The 'Group Audience' on the content node references the Group node.

the Rules Configuration

To make this work we'll have 2 Rules and 2 Rules components.

Rule 'OG subscribe after Product purchased'

Rule to subscibe a user to an Organic Group

This Rule in the selling_content Feature gives people access to the Organic Group if they completed the checkout AND made the payment for the order. In this example our 'Organic Group Rules' will act on the Rule event 'Completing the checkout process'. To make absolutely sure we only give paid users access to our group we've added a condition that checks of the order is paid. (Order balance comparison <= 0)

It's action loops over the line items of the order and executes the Rule Component 'OG subscribe line item' for each of them.

Rule Component 'OG subscribe line item'

Rule Component that subscribes line items to the OG

This Rule Component checks a couple of things in its condition:

the line item is of type 'product'

the product is of type 'Product for Organic Group'

The Organic Group Entity reference field is not empty

As action it loops over all the references field - that way we can reference multiple Organic Group nodes from one product node.

In that loop it adds the customer to the referenced Organic Group

That completed the Checkout part. You're done.

OG unsubscribe after Order Canceled

You probably want to remove users from the Organic Group when their order is cancelled for some reason - for example when they 'return' the product and want their money back.

That's exactly what the Rule 'OG unsubscribe after Order Canceled' is for, have a look:

unsubscribe user form OG when cancelling the order

When the order is changed (action) and its 'state' is set to 'Canceled' (condition) it loops over the line items of that order. For every line item a Rule Component is executed.

Rule Component 'OG unsubscribe line item'

This Rule component works almost the same as the other Rule Component. The only difference is that it UNsubscribes the user from the Organic Group instead of subscribing the user to it.

Test test test

Keep in mind that this is only an example Feature and you may need to tweak it to make it work for your specific usecase. If you have suggestions about making it better suited for certain usecases, please share below so others can benefit from your knowledge. Cheers!

 

Category: