我正在尝试创建一个限制客户购买的功能。如果客户已经购买了产品,我想禁用购买表格。
如果我只使用一个帖子ID,它的工作正常,但我不能在函数中添加多个下载。
只有一个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;
}
如何使用数组提供多次下载支持?当我使用逗号的多个帖子ID时,我无法弄清楚它只适用于第一个产品。
所以我尝试使用$in_cart = edd_item_in_cart( '50576', '50577', '50578' );
,但没有运气。
你对此有什么想法或不同方法吗?
谢谢你的帮助。