EDD Recurring Payments & Discounts: Solution
As a follow to my EDD Recurring Payments & Discounts: Proposal 1 I’d like to share my solution.
Let me set this up for you first.
Here’s the pricing options for Sprout Invoices.
Note: the customer pays the “Signup Fee” and the “Price” initially, I’ve written about a proposal to change this already.
The discount is for 25% off and multiple discount codes are created for different events. I did not create a discount per variable price.
Code
The below code will allow for a single discount code to have multiple discount rates based on the variable price the customer is purchasing.
Note: This is live code and can be compared with the variable pricing table above. Also, if I were to release a plugin or needed to use this solution more I wouldn’t hard code the discount, instead it would be calculated based on the discount options.
Don’t stop there (unless you’re not using recurring payments and just wanted to know how to have a single EDD discount code for multiple price discounts based on variable pricing). Otherwise, the discount is going to be applied to recurring rate.
The below code will remove the discount from being applied to the price (AKA recurring fee).
Note: Since these discounts are only for a single download (i.e. #49) I’m checking early.
This last bit will apply the discount to the signup fee by changing it’s fee value.
Note: The new signup fee will have the price applied. In my case that would make some signup fees a negative number, since our yearly don’t have a signup fee.
Lastly…
You’ll notice that the fees are not updated on AJAX, maybe Pippin can help me with this one since ATM I don’t have the time to dig in enough to explain with authority why. IIRC it has to do with the fees not being applied to the cart_total when AJAX is used. Anyway, I found a refresh of the cart is necessary (as a janky way to get around it).
Since I already have a customer edd_templates/checkout_cart.php I just dropped this at the end.
Note: Thank you to whomever placed triggers in the EDD jQuery functions.
Hope this helps. It took a while to figure this one out and I have a feeling it’s the best route to go. Maybe someone else has a solution.
Notes:
- Which was originally this post but I got out of hand explaining too much 🙂 ↩
Another way you can reload the cart and refresh the totals is by following the method EDD core uses for discounts and taxes.
Upon a successful ajax call, EDD actually replaces the entire HTML of the cart area with HTML returned in the ajax call:
https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/assets/js/edd-checkout-global.js#L72
That HTML is built here: https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/includes/ajax-functions.php#L297
It’s a bit janky but definitely better than trying to adjust every single value individually.
Interesting. Thanks. I’ll work on it and update the code.