Im trying to create a function to limit the purchase for customers. I want to disable purchase form if a customer has already purchased the product.
Its working fine if I use only one post ID but I cannot add multiple downloads in the function.
the working code with only one ID.
// Limit purchase per user.
add_filter( 'edd_can_checkout', 'sd_limit_purchase' );
function sd_limit_purchase(){
$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( '50576' ); // 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 $in_cart: // 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.
edd_set_error( 'already_purchased', apply_filters( 'edd_pc_error_message', __( 'You have already downloaded this plugin. There is a limit of one per customer for the free products. A free product license is lifetime and there are no site limits, please go to your account to redownload it!' ) ) );
$can_checkout = false;
break;
default:
$can_checkout = true;
break;
}
}
edd_print_errors(); // Display the errors on the checkout page.
}
}
return $can_checkout;
}
How can I give multiple downloads support using array? I cannot figure it out when I use multiple post IDs with commas its work only for the first product.
So I have tried using $in_cart = edd_item_in_cart( '50576', '50577', '50578' );
but no luck there.
Do you have any idea or different method for this?
Thankyou for your help.