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 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:

// Add user to LearnDash Group if purchases specific WooCommerce product
function jsforwp_add_user_to_group( $order_id ) {
// Add group ID from LearnDash here
$ld_group_id = XXXX;
// Add product ID from WooCommerce here
$wc_product_id = XXXX;
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
if ( $wc_product_id == $item['product_id'] ) {
ld_update_group_access( $order->customer_id, $ld_group_id, false );
add_action( 'woocommerce_order_status_completed', 'jsforwp_add_user_to_group', 10, 1 );

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.

One thought on “Add User to LearnDash Group When Orders a WooCommerce Product

  1. My Hero! I have been setting up Learn Dash with Woocommerce so that I can easily sell courses and hopefully sell full access memberships at the same time using Groups plugin with premium hookup with Woocommerce. The only problem is that buying a membership, and adding a user to a group in the Groups plugin, it will initially enroll the user in all the courses under the product. When you add a course to the product, the old user will not be enrolled in that course. To work around that, add them to the Learndash group upon membership purchase, with your hook. Then, when I create a new course, I just have to add it to that learndash group to enable access (I hope). I have a question about implementation…how do I obtain the ld group id? I can’t find it under LD Groups. I know it is called group_id, but I can’t find it. Do I have to look at my database table? Lastly, I’m not sure if it matters to you or not, but the code sample does not appear in your google amp page. Thanks again.

    Liked by 3 people

Leave a Reply

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

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

Facebook photo

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

Connecting to %s