In this case, we add a donation checkbox to the WooCommerce before order review table. The checkbox, once checked, adds a donation percentage of the subtotal to the order table totals, between the subtotal and the total.
The checkbox updates dynamically using ajax so as soon as its checked, the donation fee is displayed in the totals table as seen in the above gallery images and demo video.
The donation is added as a fee in the backend on the Edit Order details box ( optional ) as well as the list items table and totals table as seen in this screenshot :
You can :
- Set the percentage in the code.
- Modify the text in the totals table.
- Modify the checkbox text.
- Make the checkbox optional or compulsory.
The code also includes a conditional tag which you can modify or remove. The conditional tag displays the checkbox for a specific user role so only logged in users with that role can donate a percentage of the subtotal to charity.
If you want the checkbox to display for everyone, simply remove the conditional tag from the code using the instructions below.
Copy and paste the PHP code to your child themes functions file or custom code snippets plugin.
Optional : Copy and paste the CSS to the end of your child themes style sheet and clear caching. You may need this to fix the float issue on some themes.
The code includes a conditional tag for administrators user role. If you want to use another role, swap out the slug for the role on lines 13 and 52.
If you don’t want to use a conditional for user, remove or comment out the code on lines 13, 27 and this code on line 52.
Modify the :
- Donate to charity text on line 21.
- User Role Donation text on line 56.
- Percentage donation on 71.