• Resolved Harsh Goyal

    (@harshgoyaa)


    Hi team,

    i wanted to know if the below is possible.

    i will be creating a website using Ultimate Member – User Profile, User Registration, Login & Membership Plugin, and use the registration page to get all the details that i want. Now depending on the membership plan the client uses, i would be giving them access to the details of other people. for instance i would like to hide the contact details of all clients and depending the membership plan, i would allow them to see the contact details of other users.

    Example

    GOLD- 30 users
    silver – 15 users
    now if someone is gold member, he/ she is allowed to see the contact details(hidden data) of only 30 users, which he/she can chose from the list of users in all users. after 30 seeing the hidden/ contact details of 30 users, the member has to take the member ship again.
    we are assuming the content which is hidden is the contact details and will be available based on the membership plan.

    i know its bit complicated, but if you get it please let know. i am ready to buy the paid version

Viewing 15 replies - 16 through 30 (of 33 total)
  • Thread Starter Harsh Goyal

    (@harshgoyaa)

    hi @champsupertramp

    i have done all the mentioned above,
    there is a issue, i am not sure why it is happening.

    for now i have given gold -11 view and silver – 10 views attribute as u mentioned of type(name (numeric)).

    i am only able to view gold – 5 views and silver 4 views. plus every time i try to see other profile after this, it diverts back to the active user profile. i have tried increasing the view count but the same result.

    i have made a reached-limit page as well. i have 150+ sample users set for testing.

    could you please check it out.

    used this code for creating table

    function function_to_install_db() {
      global $wpdb;
       
      // Let's not break the site with exception messages
      $wpdb->hide_errors();
       
      if ( ! function_exists( 'dbDelta' ) ) {
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
      }
     
      $collate = '';
     
      if ( $wpdb->has_cap( 'collation' ) ) {
        $collate = $wpdb->get_charset_collate();
      }
       
      $schema = "
    CREATE TABLE {$wpdb->prefix}wp_custom_visited_profiles (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      user_id integer,
      visited_user_id integer
      visited_date datetime
    ) $collate;";
     
      dbDelta( $schema );
    }
    • This reply was modified 3 years, 1 month ago by Harsh Goyal.
    • This reply was modified 3 years, 1 month ago by Harsh Goyal.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @harshgoyaa

    There’s an issue with your database table creation. You should change it from

    {$wpdb->prefix}wp_custom_visited_profiles

    to

    {$wpdb->prefix}custom_visited_profiles.

    Let me know if this is causing the issue.

    Regards,

    Thread Starter Harsh Goyal

    (@harshgoyaa)

    hi @champsupertramp,

    nope the issue still persists.

    wocom attribute which i created is

    um_view_profile_limit – order by(custom ordering) – config items(10,20)
    i have also tried – order by(Name Numeric)

    copied the same code as above and placed it in the function.php of the child theme
    create table command is also placed in the same function.php

    created a “reach-limit” page as well.

    hope you will be able to crack it

    also,
    i wanted to know is thr a place we can view how many profiles have been viewed and how many they have left(this can be useful for both the admin and user )

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @harshgoyaa

    Please try this updated code snippet. Let me know if you’re still encountering issues. Drop the existing custom_visited_profiles table and ensure that the test account has um_view_profile_limit and um_total_visited_profiles set to 0 before you test the code. Create a new Profile Form field that disallows users to edit it to display the total viewed profile. Use um_total_visited_profiles as the meta key/field name of that field.

    
     add_action("init", function(){
        um_013122_install_db();
     });
     function um_013122_install_db() {
        global $wpdb;
         
        // Let's not break the site with exception messages
        $wpdb->hide_errors();
         
        if ( ! function_exists( 'dbDelta' ) ) {
          require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        }
       
        $collate = '';
       
        if ( $wpdb->has_cap( 'collation' ) ) {
          $collate = $wpdb->get_charset_collate();
        }
         
        $schema = "
        CREATE TABLE {$wpdb->prefix}custom_visited_profiles (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            user_id integer,
            visited_user_id integer
            visited_date datetime
        ) $collate;";
        
        dbDelta( $schema );
      }
    
    add_action("template_redirect","um_custom_visit_profile", 99999 );
    function um_custom_visit_profile(){
        if( ! class_exists("UM") ) return;
       
        if( ! um_is_myprofile() && um_is_core_page("user") ){
    
            global $wpdb;
            $visiting_user_id = um_get_requested_user();
            $user_id = get_current_user_id();
           
            // GET USER ORDERS (COMPLETED + PROCESSING)
            $customer_orders = get_posts( array(
                'numberposts' => -1,
                'meta_key'    => '_customer_user',
                'meta_value'  => $user_id,
                'post_type'   => wc_get_order_types(),
                'post_status' => array_keys( wc_get_is_paid_statuses() ),
            ) );
           
           
            if ( ! $customer_orders ) return;
            
            // Get all user's purchased products and sum all the limit
            $limit = 0;
            foreach ( $customer_orders as $customer_order ) {
                $order = wc_get_order( $customer_order->ID );
                $items = $order->get_items();
                foreach ( $items as $item ) {
                    $product_id = $item->get_product_id();
                    $p = new WC_Product( $product_id );  // create an object of WC_Product class
                    $limit += (int)$p->get_attribute( 'um_view_profile_limit' );  // call get_attribute method
                }
            }
    
            update_user_meta( $user_id, "um_view_profile_limit", $limit );
                
    
           $has_row = $wpdb->get_row( $wpdb->prepare(
               "SELECT * FROM {$wpdb->prefix}custom_visited_profiles WHERE user_id = %d  AND visited_user_id = %d", $user_id, $visiting_user_id ) 
            );
    
            $total_visited = (int) get_user_meta( $user_id, "um_total_visited_profiles", true );
            
            $total_limit = (int) $limit;
           
            if( $total_visited >= $total_limit ){ // If the user rechead the viewing limit, redirect them to a custom page
                wp_redirect("/reached-limit"); exit;
            }
    
            if( ! $has_row ){ // if user hasn't visited this profile, add the viewing profile in the currently logged-in user's record
                $table = $wpdb->prefix.'custom_visited_profiles';
                $data = array('user_id' => $user_id, 'visited_user_id' => $visiting_user_id );
                $wpdb->insert( $table, $data );
                $results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}custom_visited_profiles WHERE user_id = %d ", $user_id ) );
                update_user_meta( $user_id, "um_total_visited_profiles", count( $results ) );
            }
    
        }
    }
    

    Regards,

    Thread Starter Harsh Goyal

    (@harshgoyaa)

    hi @champsupertramp

    i have tried the above. i am able to see the profile limit in the profile page. i created a new form and registered and bought a plan. so now the maximum view is updating properly but the views left, is not updating. plus the old issue still remains, not able to see more than 2-3 profiles. i did check my myphpadmin and could not find the table only (custom_visited_profiles). not sure what that is all about.

    hoping for the best ??

    thanks and regards,
    Harsh Goyal

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @harshgoyaa

    Could you please confirm that the table exists wp_custom_visited_profiles in the database? Try disabling the UM User Cache and then see if the views left field is updating. To disable the cache, please go to WP Admin > Ultimate Member > Settings > Misc > see “Disable Cache User Profile”.

    Regards,

    Thread Starter Harsh Goyal

    (@harshgoyaa)

    hi @champsupertramp

    nope i could not find the table in myphpadmin database. yep i had already disabled the UM user cache.

    table not being present could be the root of this problem

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @harshgoyaa

    There was an error with the table creation in the above code. Please try the following:

    
     add_action("init", function(){
        um_013122_install_db();
     });
     function um_013122_install_db() {
        global $wpdb;
         
        // Let's not break the site with exception messages
        $wpdb->hide_errors();
         
        if ( ! function_exists( 'dbDelta' ) ) {
          require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        }
       
        $collate = '';
       
        if ( $wpdb->has_cap( 'collation' ) ) {
          $collate = $wpdb->get_charset_collate();
        }
         
        $schema = "
        CREATE TABLE {$wpdb->prefix}custom_visited_profiles (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            user_id integer,
            visited_user_id integer,
            visited_date datetime
        ) $collate;";
        
        dbDelta( $schema );
      }

    Regards,

    Thread Starter Harsh Goyal

    (@harshgoyaa)

    hi @champsupertramp

    Hard luck. still facing the issue and the table is still nor created ??

    Thanks and regards
    Harsh

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @harshgoyaa

    I found the issue with creating the table. Please try the following:

    add_action("init", function(){
        um_013122_install_db();
     });
     function um_013122_install_db() {
        global $wpdb;
         
        // Let's not break the site with exception messages
        //$wpdb->hide_errors();
         
        if ( ! function_exists( 'dbDelta' ) ) {
          require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        }
       
        $collate = '';
       
        if ( $wpdb->has_cap( 'collation' ) ) {
          $collate = $wpdb->get_charset_collate();
        }
         
        $schema = "
        CREATE TABLE {$wpdb->prefix}custom_visited_profiles (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            user_id integer,
            visited_user_id integer,
            visited_date datetime,
            primary key (id)
        ) $collate;";
        
        dbDelta( $schema );
    }

    Regards,

    Thread Starter Harsh Goyal

    (@harshgoyaa)

    hi @champsupertramp,

    yes this looks good, now the numbers also updating perfectly. table is also created in the database i manually verified. now the thing is i am not able to check/ open all the profiles. for gold membership i gave 20 as limit, but the i am able visit only 4 profiles ?? hoping we can solve this and get this over with.

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @harshgoyaa

    Could you please share all the code that you have for this feature? I’ll review them and test them on my end.

    Regards,

    Thread Starter Harsh Goyal

    (@harshgoyaa)

    hi @champsupertramp

    here is the code

    add_action("init", function(){
        um_013122_install_db();
     });
     function um_013122_install_db() {
        global $wpdb;
         
        // Let's not break the site with exception messages
        //$wpdb->hide_errors();
         
        if ( ! function_exists( 'dbDelta' ) ) {
          require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        }
       
        $collate = '';
       
        if ( $wpdb->has_cap( 'collation' ) ) {
          $collate = $wpdb->get_charset_collate();
        }
         
        $schema = "
        CREATE TABLE {$wpdb->prefix}custom_visited_profiles (
            id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            user_id integer,
            visited_user_id integer,
            visited_date datetime,
            primary key (id)
        ) $collate;";
        
        dbDelta( $schema );
    }
    
    add_action("template_redirect","um_custom_visit_profile", 99999 );
    function um_custom_visit_profile(){
        if( ! class_exists("UM") ) return;
       
        if( ! um_is_myprofile() && um_is_core_page("user") ){
    
            global $wpdb;
            $visiting_user_id = um_get_requested_user();
            $user_id = get_current_user_id();
           
            // GET USER ORDERS (COMPLETED + PROCESSING)
            $customer_orders = get_posts( array(
                'numberposts' => -1,
                'meta_key'    => '_customer_user',
                'meta_value'  => $user_id,
                'post_type'   => wc_get_order_types(),
                'post_status' => array_keys( wc_get_is_paid_statuses() ),
            ) );
           
           
            if ( ! $customer_orders ) return;
            
            // Get all user's purchased products and sum all the limit
            $limit = 0;
            foreach ( $customer_orders as $customer_order ) {
                $order = wc_get_order( $customer_order->ID );
                $items = $order->get_items();
                foreach ( $items as $item ) {
                    $product_id = $item->get_product_id();
                    $p = new WC_Product( $product_id );  // create an object of WC_Product class
                    $limit += (int)$p->get_attribute( 'um_view_profile_limit' );  // call get_attribute method
                }
            }
    
            update_user_meta( $user_id, "um_view_profile_limit", $limit );
                
    
           $has_row = $wpdb->get_row( $wpdb->prepare(
               "SELECT * FROM {$wpdb->prefix}custom_visited_profiles WHERE user_id = %d  AND visited_user_id = %d", $user_id, $visiting_user_id ) 
            );
    
            $total_visited = (int) get_user_meta( $user_id, "um_total_visited_profiles", true );
            
            $total_limit = (int) $limit;
           
            if( $total_visited >= $total_limit ){ // If the user rechead the viewing limit, redirect them to a custom page
                wp_redirect("/reached-limit"); exit;
            }
    
            if( ! $has_row ){ // if user hasn't visited this profile, add the viewing profile in the currently logged-in user's record
                $table = $wpdb->prefix.'custom_visited_profiles';
                $data = array('user_id' => $user_id, 'visited_user_id' => $visiting_user_id );
                $wpdb->insert( $table, $data );
                $results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}custom_visited_profiles WHERE user_id = %d ", $user_id ) );
                update_user_meta( $user_id, "um_total_visited_profiles", count( $results ) );
            }
    
        }
    }
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @harshgoyaa

    When you reached 4, does it redirect you to the page “/reached-limit”?

    Regards,

    Thread Starter Harsh Goyal

    (@harshgoyaa)

    hi @champsupertramp,

    nope it redirects to logged in user profile.

    example:

    if its me, i can visit 4 profile and 5th will redirect to my profile it self.

    Regards,

Viewing 15 replies - 16 through 30 (of 33 total)
  • The topic ‘Restrict content for certain number of users’ is closed to new replies.