Update: thanks to many of you and your remarks I’ve updated my code to make it compatible with the new LearnDash and solve several other problems. To keep it easy to understand, I’ve left this article untouched and added the updated code in the “Code Update” section you can access at the end of the article (or just click here).
Recently, a client asked for a drip-fed course sold by installments throughout the drip-feeding. The client would get a bill each time a lesson would go live.
I did my research and I found no viable option for paying by installments in a clean way. I wanted to keep the backend easy to manage and enjoy the power of WooCommerce.
I saw screenshots of WooCommerce Subscriptions and thought it was the perfect solution.
I bought it and started to configure everything. It looked freakin’ good but wasn’t really working for my purpose. So I had to take some time to make a custom solution and I thought I would share it here.
This will be a two-part article series. The first part will be dedicated to making WooCommerce Subscriptions and LearnDash compatible. The second part will talk about modifying the plugin to allow installment payments.
Warning: this article will be more technical than the rest of the blog.
So, what do you need?
- a WordPress
- WooCommerce plugin
- LearnDash plugin
- LearnDash WooCommerce Add-on
- WooCommerce Subscriptions
Once you’ve got everything installed in a dev environment, we’ll dive into the code.
Selling a course through a subscription
Before hacking the source code, there is a simple manipulation to sell your courses in a subscription. You won’t be able to do anything else than giving access to the course, though.
Here are the steps:
- Create a new WooCommerce product
- Select “Course” in product type
- Select the course(s) you want to sell
- Save the draft
- Change the product type to subscription
- Configure the subscription
- Hit publish
What happens is, switching to a subscription doesn’t remove the associated courses. But again, if you’re selling this to a client, it’s not elegant so let’s code something to fix this shall we?
Note: all the code will be done directly on the LearnDash WooCommerce extension. After discussing the matter on their support forums, it seemed like the cleanest option.
Note 2: I’m not an extremely efficient coder, if you think my code can be optimized, comment below so we can improve it :)
We want to go from this:
And it’s fairly easy when you look at the source code. It’s handled by CSS rules. You can trick them by adding “show_if_simple” in the render_course_selector() function.
Giving and removing access to courses when the subscription state changes
So now, we can sell courses on a subscription basis. But if someone cancels or puts on hold the payments, he still has access to the course. And this sucks because you get robbed. So how to fix this?
WooCommerce Subscription has some nice action hooks to take care of this. Let’s head over to the LearnDash WooCommerce constructor to take care of the following:
- on hold
- expired: this one triggers when the membership expires if there’s a limit to it. Keep this in mind for the next article ;)
Now let’s create two functions to give or remove access. Both functions will be similar. The first part is to find the courses corresponding to the subscription. The second gives or removes access to the course.
The code above is very useful to understand what I did to get WooCommerce Subscriptions and LearnDash working together, but there was a couple of problems (even without WooSubs and LearnDash both updating their code):
- if you were drip-feeding your course, it would reset the subscription date to the course every time the subscription would be billed. So for a weekly course with a weekly subscription, your subscribers wouldn’t go past the first week (but still get billed each week) which was a big issue for me and my client and my client’s clients
- the code was sometimes putting duplicates in the access list which is not really clean
The new code below solves all of this.
Warning: I’m more a technical marketer than a developer. What I do work but is probably not the most efficient way to do so. Some parts are just a repeat of the course access management function provided in the LearnDash source code. If you have ideas or improvements in mind, feel free to use the comments, happy to update :)
The New send_receipt() function
Each time a transaction go through, send_receipt() is triggered and updates the course access list and the date the subscriber gained accessed to the course, so I needed to modify this.
The new give_course_access() function
Same thing, I basically check the access list before giving access to the course, in order to keep the date the subscriber accessed the course the same and avoid resetting its access (and messing with the drip-fed access).
That’s all folks
Yep, it’s done. So of course, we can add options to make it easier to customize, but this should get you started.
The next article will be about customizing WooCommerce Subscription to sell by installments.
If you have comments specific to the code or if you have questions feel free to hit me up in the comments below :)