Discover in this tutorial how to show the total revenue of payments made with Stripe using a WordPress shortcode.
If you or any of your clients have an online business that uses Stripe as a payment gateway and is a lover of Open Metrics, this will surely interest you.
In this tutorial you are going to learn how to show the number of total revenue you have achieved through Stripe using its fantastic API, and keep that number always updated wherever you want on the web.
Here’s how to do it:
Instructions to create a shortcode to display the total revenue number from Stripe
1. Create the shorcode
Add the following code at the end of functions.php or in your functionality plugin:
In this code you use the Stripe library and the secret API key stored by Restrict Content Pro.
If you are using another plugin (Easy Digital Downloads, Gravity Forms…) you will have to change that part and adapt it to it. If you want me to prepare a tutorial with some variation just let me know.
The Stripe API does not have an endpoint where you can get the total accumulated revenue, so you have to get all the charges made correctly (using a loop with the
autoPagingIterator function), add them and subtract the returns.
This depending on the project can be quite a “heavy” request, so inside the shortcode you create and use a transient, to cache the result for a day (although you can modify it to your liking). This way you make sure that the result is displayed immediately.
Finally, you divide the amount by 100 (since Stripe works internally with the amounts multiplied by 100), round the figure so that it does not show decimals and add the dollar symbol ($).
2. Insert the shortcode where you want
Finally, just type
[stripe_revenue] where you want it to be displayed.
Now you know how to display the total revenue collected through Stripe and always keep it updated at any point of the web using a shortcode.
Even if you are not going to use this snippet to display the data publicly, you can use it to create a dashboard where you can add all the relevant data of your business.
In addition, with small variations you can customize it to fit 100% to your needs.
Now you just have to put it into practice. 😉
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂