Last active
September 29, 2018 15:07
-
-
Save adriannees/900242b5f19d9e1123a1708232595184 to your computer and use it in GitHub Desktop.
Limit downloads on EDD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add this to the functions.php file on your child theme | |
/** Limit download purchases to one per account | |
* HT to Scott Deluzio for original code this is modified from https://scottdeluzio.com/easy-digital-downloads-limit-one-per-customer/ | |
// Give nag message if customer has bought download previously | |
add_filter( 'edd_can_checkout', 'sd_set_purchase_limit' ); | |
function sd_set_purchase_limit(){ | |
$user_id = get_current_user_id(); // Get the current customer's ID | |
$downloads = edd_get_users_purchased_products( $user_id ); // Get all of the products the customer has purchased in the past | |
$can_checkout = true; // Allow checkout for now | |
$in_cart = edd_item_in_cart( '57' ); // Check to see if our download is in the customer's cart. Change 57 to your download's ID. | |
$download_name = get_the_title( '57' ); // Get the name of the download. Change 57 to your download's ID. | |
if ( $in_cart == true ){ // If the item isn't in the customer's cart we don't need to go any further. | |
if ( ! empty( $downloads ) ){ // If the customer hasn't purchased anything before we don't need to go any further. | |
foreach ( $downloads as $download ){ // Loop through each product the customer has purchased before. | |
switch ( $download->ID ) { | |
case '57': // If the customer has purchased the product we want to limit one per customer set an error message. Again change 57 to your product's ID. Update URL in nag message | |
$can_checkout = false; | |
edd_set_error( 'already_purchased', apply_filters( 'edd_pc_error_message', __( 'You have already purchased "'. $download_name . '". To download the file again visit <a href="#">your dashboard</a>. If there are other items in your cart you would like to purchase, remove "'. $download_name . '" to continue.' ) ) ); | |
break; | |
default: | |
$can_checkout = true; | |
edd_unset_error( 'already_purchased' ); | |
break; | |
} | |
} | |
edd_print_errors(); // Display the errors on the checkout page. | |
} | |
} | |
return $can_checkout | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @adriannees,
I got here from scottdeluzio.com post about this.
I was wondering if you know how to make this code to check all the downloads on a specific category, so every time I add a download to that category I don't have to edit the code with new ID's?
Thanks
All the best.