LearnDash and WordPress Groups

Add User to LearnDash Group When Orders a WooCommerce Product

LearnDash and WordPress Groups

I love the LearnDash LMS for WordPress.  I use it over at javascriptforwp.com and not only is it powerful out of the box and with plugins, but it’s hooks allow for even more.

One thing LearnDash cannot do out of the box is sell access to Groups via WooCommerce.

A Group in LearnDash, amongst other things, let’s you assign a group of courses to a group of users.  You can add courses over time and those users will automatically get access.

For my JavaScript for WordPress Master Course I wanted to switch from one gigantic course (hundreds of lessons) to a bunch of smaller courses that I could also sell individually.

To do this I setup a new Group in LearnDash called “Master Course” and added my new smaller courses to it.  I also created a new WooCommerce Virtual Product called “Master Course.”

Then I simply added this code to my functions.php:

Here is what this code does:

  • Hook into WooCommerce when an order is marked complete
  • Checks to see if that order includes my Master Course WooCommerce product
  • If so, it adds that user who purchased the product to my LearnDash group

It’s pretty simple and shows the power of hooks in WordPress.

To customize this for your own use you would have to add the Group and Product IDs to the corresponding variables on line 5 and 7.

One thing this doesn’t do is remove a person from a group if they cancel or are refunded the order, since this is something I wanted to do manually.  However, you could easily do this by hooking into the woocommerce_order_status_cancelled or woocommerce_order_status_refunded hooks.

A big thanks to Justin from LearnDash and Ernest from UncannyOwl, who is working on a plugin that will also allow for this functionality.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s