dousuiguang9328 2018-03-20 14:30
浏览 45

每个客户代码一次购买多次下载支持 - Easy Digital Downloads

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题