Hi @harshgoyaa
yeah, I would create a database table to save the visited profile.
Here’s the table structure for the custom table:
Table structure( table name: wp_custom_visited_profiles
:
– id ( BIGINT – auto increment )
– user_id ( INT )
– visited_user_id (INT )
– visited_date ( datetime )
In WooCommerce product settings, add an attribute named um_view_profile_limit
with the viewing limit number e.g. 30
So when a user visits a profile, I will use the following code snippet to check if the currently logged-in user visits a profile. It will redirect the user to a custom page /reached-limit/
when the user has reached the limit number set in the product settings. Please note that you can have multiple purchased products. The code below will sum up the limit from different products for the currently logged-in user:
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
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 visiting_user_id = %d", $user_id, $visiting_user_id )
);
$total_visited = (int) get_user_meta( $user_id, "um_total_visited_profiles", true );
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, 'visiting_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 ) );
}
$total_limit = (int) get_user_meta( $user_id, "um_view_profile_limit", true );
if( $total_visited >= $total_limit && ! $has_row ){ // If the user rechead the viewing limit, redirect them to a custom page
wp_redirect("/reached-limit"); exit;
}
}
}
Regards,