Skip to content

Instantly share code, notes, and snippets.

@ScottDeLuzio
Created May 21, 2019 18:29
Include multiple download IDs in limit one per customer in EDD
add_action( 'edd_before_purchase_form', 'sd_set_purchase_limit_error' );
function sd_set_purchase_limit_error(){
$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
$download_ids = array( 1,2,3 ); //Whatever the IDs are that you want to check for
foreach ( $download_ids as $download_id ){
$in_cart = edd_item_in_cart( $download_id ); // Check to see if our download is in the customer's cart. 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 '1':
case '2':
case '3': // If the customer has purchased the product(s) we want to limit one per customer set an error message.
edd_set_error( 'already_purchased', apply_filters( 'edd_pc_error_message', __( 'You have already purchased the one-per-customer plugin. There is a limit of one per customer. You can log out and purchase with a different account, or remove it from your cart with the link above.' ) ) );
break;
default:
edd_unset_error( 'already_purchased' );
break;
}
}
edd_print_errors(); // Display the errors on the checkout page.
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment