Learn in this tutorial how to show a buy button only to users who have not purchased that product in Easy Digital Downloads (EDD) yet.
Besides creating a page with the product or download, Easy Digital Downloads allows you to use shortcodes to place the buy button wherever you want.
This gives you a lot of versatility when it comes to displaying the button, but it has a downside: by doing so the button is shown to both users who have purchased the product and those who have not.
Do you want to avoid displaying the buy button to customers who have already purchased the product or download?
Read on. ๐
Steps to hide the buy button from users who have already purchased the product on Easy Digital Downloads
To get the buy button to show conditionally you need to create a new shortcode containing the original shortcode.
Yes, like a matryoshka, exactly. ๐
1. Create a conditional shortcode
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this case you use the edd_has_user_purchased
function to detect if the current users have already purchased that product.
If they have done so, the shortcode with the button will not be shown, and if they have not yet purchased it, it will be shown.
You have to replace [purchase_link id="3115" text="Buy" style="button" color="blue"]
with the shortcode you want to show and change the $download_id
to be the same (3115, in this case).
2. Enter the shortcode
Now you just need to use the new shortcode you created where you want to display the buy button by typing [shortcode EDD]
in the place where you want it to be displayed.
Important:
- If you are using the classic editor, use the HTML tab to enter the shortcode.
- In case you are already using the block editor (Gutenberg) you can add it to the normal paragraph block, the HTML block or the shortcode block. All of them will render the content correctly.
Conclusions
Do you want to prevent the EDD buy button from being shown to users who have already purchased the product? Now you know how to do it with a conditional shortcode.
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. ๐