• Hello Dear @champsupertramp
    as per previous conversation in other post of controling number of new registered users that logged in user can add
    but please put in consideration that will use the snippets that provided in the previous post

    need to add field (new users limit) in edit user profile page in back end or even in UM profile page
    this field I can add in it number of users the current user can register while he is logged in using UM register form
    so if I add for specific user in this field number 4 that mean he can add only 4 new users and then he will not be able to add more and if I change this number to be 20 he will get the ability again until he reach 20 registered new user then he will be not able to add more

    best regards

Viewing 15 replies - 16 through 30 (of 46 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Thanks for providing the json file for Code snippets.

    Which code snippets are active on your site?

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    all related to UM and all is active
    each one has specific thing to do
    regards

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    @champsupertramp
    I did big test to make it easy to you
    these is the 3 codes snippets
    logged in user can add new user + limit registration number

    // Enable Register form for logged-in users
    add_filter( 'um_registration_for_loggedin_users', '__return_true' );
    
    // Add current logged-in user as a referer
    add_action( 'um_registration_complete', 'um_110821_user_referral' ,1 );
    function um_110821_user_referral( $user_id ){
        $referer_id = get_current_user_id();
        update_user_meta( $user_id, "um_user_referral", $referer_id );
    }
    
    // Validate registration limit
    add_action("um_submit_form_register","um_110821_validate_registration_limit", 1 );
    function um_110821_validate_registration_limit( $post_form ){
      
            $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => get_current_user_id(),
                    "fields" => "ids"
                    )
                )
            );
    	
    	    if( UM()->form()->errors['user_email'] === "You must provide your email" ){
               unset( UM()->form()->errors['user_email'] );
    		}
    
              $user_limit = (int)um_user("registration_limit");
              if( $users > $user_limit ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           
    
            if( empty( UM()->form()->errors ) ){
                unset( UM()->form()->errors );
            }
    
    }

    display total registered users in profile

    add_shortcode("um_display_total_referral", "um_112521_display_total_referral");
    function um_112521_display_total_referral(){
             $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => um_user("ID"),
                    "fields" => "ids"
                    )
                )
            );
    
            return $users;
    }

    reset registered users number in UM profile

    add_shortcode("um_reset_referral","um_112521_reset_referral_button");
    function um_112521_reset_referral_button(){
    
        return "<a href='?um_reset_referral=".um_user("ID")."' class='um-button'>Reset Referral</a>";
    }
    
    add_action("template_redirect","um_112521_reset_referral");
    function um_112521_reset_referral(){
      
        if( isset( $_REQUEST['um_reset_referral'] ) && current_user_can("manage_options") ){
            global $wpdb;
            $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->usermeta} WHERE meta_key = 'um_user_referral' AND meta_value = %d ", $_REQUEST['um_reset_referral'] ) );
        }
    }

    the result :-
    1- when using first code with second one
    adjusting register limit to 1 = system prohibted from adding users after register 2 user
    if limit =2 then prohption after 3 users

    3- activate snippets of reset button caused automatically reset the number of registered users to be 0 that without pressing the button
    and when limit = 1 it stopped after adding 3 users then refuse to add new users
    and that without changing number of registered users counter its still 0

    and if click on the button again to do reset then user can add 3 users instead of 1 then its stopped

    so please help

    [Moderator note: Please, No bumping].

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Sorry for the late response. I was busy with other topics. I’ll review the code above and let you know my findings.

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    okay thank you

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Where do you add the shortcodes [um_reset_referral] and [um_display_total_referral]? Are they on the same page where the Register form shortcode is added?

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Hi @champsupertramp
    no I added both in the profile form
    Regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Please test these codes. This should only allow a user to register 3 users. Please note: I’ve changed this $user_limit = (int)um_user("registration_limit"); to $user_limit = 3 to test the issue.

    // Enable Register form for logged-in users
    add_filter( 'um_registration_for_loggedin_users', '__return_true' );
    
    // Add current logged-in user as a referer
    add_action( 'um_registration_complete', 'um_110821_user_referral' ,1 );
    function um_110821_user_referral( $user_id ){
        $referer_id = get_current_user_id();
        update_user_meta( $user_id, "um_user_referral", $referer_id );
    }
    
    // Validate registration limit
    add_action("um_submit_form_register","um_110821_validate_registration_limit", 1 );
    function um_110821_validate_registration_limit( $post_form ){
      
            $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => get_current_user_id(),
                    "fields" => "ids"
                    )
                )
            );
    	
    	    if( UM()->form()->errors['user_email'] === "You must provide your email" ){
               unset( UM()->form()->errors['user_email'] );
    		}
    
              $user_limit = 3;
              if( $users >= $user_limit ){
                UM()->form()->add_error('user_email', __( 'You are not allowed to add more users', 'ultimate-member' ) );
               }
           
    
            if( empty( UM()->form()->errors ) ){
                unset( UM()->form()->errors );
            }
    
    }
    
    add_shortcode("um_display_total_referral", "um_112521_display_total_referral");
    function um_112521_display_total_referral(){
             $users = count( get_users(
                    array(
                    'meta_key' => "um_user_referral",
                    'meta_value' => um_user("ID"),
                    "fields" => "ids"
                    )
                )
            );
    
            return $users;
    }
    
    add_shortcode("um_reset_referral","um_112521_reset_referral_button");
    function um_112521_reset_referral_button(){
    
        return "<a href='?um_reset_referral=".um_user("ID")."' class='um-button'>Reset Referral</a>";
    }
    
    add_action("template_redirect","um_112521_reset_referral");
    function um_112521_reset_referral(){
      
        if( isset( $_REQUEST['um_reset_referral'] ) && current_user_can("manage_options") ){
            global $wpdb;
            $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->usermeta} WHERE meta_key = 'um_user_referral' AND meta_value = %d ", esc_attr( $_REQUEST['um_reset_referral'] ) ) );
    		 wp_redirect( $_SERVER['HTTP_REFERER'] ); exit;
        }
    }

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Dear @champsupertramp
    thank you very much its work now, you are the best
    best regards and wish you all the best

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Thanks for letting us know. And sorry that it took some time – thank you for your patience. I’m marking this as resolved now.

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    @champsupertramp don’t worry I thank you for this great helping
    best regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Thanks for letting us know.

    Regards,

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Dear @champsupertramp
    now we can limit number of new registered users the current user can add
    now my question
    is it possible to add field name “additional register limit” to Ultimate Member: content restriction section that exist in any product
    so if we write in this field as example = 10
    and User X has register limit = 3
    then User X purchase this product then his register limit will be 3 + 10 = 13

    would you please help to do this
    best regards

    Thread Starter aymanibrahim77

    (@aymanibrahim77)

    Dear @champsupertramp answer please ?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @aymanibrahim77

    Happy New Year!

    Instead of adding a custom field in the product, try adding an attribute value. Please see the screenshot: https://drive.google.com/file/d/177HBdU7AlJYJy3PB91tIBSUYCT2HWG1D/view?usp=sharing

    And then I would retrieve that attribute to add it to the Register limit that will be saved to the user’s meta registration_limit on the completed order. Please try the following code snippet to allow you to top up a user’s registration limit when the customer purchases a product that has the attribute.

    function um_010322_woocommerce_order_status_completed( $order_id ) {
    
        $order = wc_get_order( $order_id );
        $user_id = $order->get_user_id(); // or $order->get_customer_id();
    
        // iterate through order items/products
        foreach ($order->get_items() as $item) {
            $pid = $item['product_id'];  // returns the product id
    
            $p = new WC_Product( $pid );  // create an object of WC_Product class
            
            $register_limit = (int)$p->get_attribute( 'um_user_register_limit' );  // call get_attribute method
            $current_register_limit = (int) um_user("registration_limit");
            update_user_meta( $user_id, "registration_limit", $register_limit + $current_register_limit );
        }
    
    }
    add_action( 'woocommerce_order_status_completed', 'um_010322_woocommerce_order_status_completed', 10, 1 );

    Regards,

    • This reply was modified 3 years, 2 months ago by Champ Camba.
Viewing 15 replies - 16 through 30 (of 46 total)
  • The topic ‘control numbers of new users’ is closed to new replies.