Limit visibility of Statistics by Membership

I would like to create two membership plans. One that can access the statistics of their listings and one that can’t. Is that possible with the two extensions? If so, how?

Please try this PHP snippet. Please change membership_plan_id with plan id which should hide the statistics menu item for a vendor

add_filter( 
	'hivepress/v1/menus/listing_manage/items', 
	function ($items, $menu){
		if ( isset( $items['listing_edit'] ) && hivepress()->get_version( 'memberships' ) ) {
			
			$listing = $menu->get_context( 'listing' );

			if ( hivepress()->helper->is_class_instance( $listing, '\HivePress\Models\Listing' ) && $listing->get_status() === 'publish' ) {
				if(is_user_logged_in() && $listing){
					$membership = \HivePress\Models\Membership::query()->filter(['user' => get_current_user_id()])->get_first();
			
					if(!$membership || membership_plan_id === $membership->get_plan__id()){
						unset($items['listing_statistics']);
					}
				}
			}
		}

		return $items;
	}, 
	1000, 
	2 
);
1 Like

Not sure what I is wrong.

Please make sure that you have changed membership_plan_id with plan id which should hide the statistics menu item for a vendor. It is possible to take the plan id from the browser address bar where it is the number between ?post= and &action=

Thank you yenhen. I understand now. I was trying to use the name of the membership plan. I didn’t know it assigned an ID number. Putting in the ID hid the statistic link when viewing the listing. However, statistics is still available for the vendor by going to listings and then clicking the statistics icon in between the favorite star icon and the view icon. So the code only works for 1 of 2 places.

I am trying to use the Statistics feature as an upgrade to another membership. So ideally, I would like the Statistics icon on the users listings page and the Statistics link on individual listing be visible and clickable. If you have the correct membership the statistics will show. If you don’t have the correct membership it brings up the window telling them that feature requires a difference membership. Would that be possible with a snippet or would it require customization? Sorry I didn’t explain what I was trying to do or give you context. Thank you for all you do.

1 Like

Please try this PHP snippet instead

add_filter( 
	'hivepress/v1/menus/listing_manage/items', 
	function ($items, $menu){
		if ( isset( $items['listing_edit'] ) && hivepress()->get_version( 'memberships' ) ) {
			
			$listing = $menu->get_context( 'listing' );

			if ( hivepress()->helper->is_class_instance( $listing, '\HivePress\Models\Listing' ) && $listing->get_status() === 'publish' ) {
				if(is_user_logged_in() && $listing){
					$membership = \HivePress\Models\Membership::query()->filter(['user' => get_current_user_id()])->get_first();
			
					if(!$membership || membership_plan_id === $membership->get_plan__id()){
						unset($items['listing_statistics']);
					}
				}
			}
		}

		return $items;
	}, 
	1000, 
	2 
);

add_filter( 
	'hivepress/v1/templates/listing_edit_block/blocks', 
	function ($blocks, $template){
		if ( hivepress()->get_version( 'memberships' ) ) {
			
			$listing = $template->get_context( 'listing' );

			if(is_user_logged_in() && $listing){
				$membership = \HivePress\Models\Membership::query()->filter(['user' => get_current_user_id()])->get_first();
		
				if(!$membership || membership_plan_id === $membership->get_plan__id()){
					$blocks = hivepress()->helper->merge_trees(
					[ 'blocks' => $blocks ],
					[
						'blocks' => [
							'listing_statistics_link' => [
								'type' => 'content',
							],
						],
				]
				)['blocks'];
				}
			}
		}

		return $blocks;
	}, 
	1000, 
	2 
);
2 Likes

If I wanted to block it for multiple membership id’s can I just separate them with a comma or do I need to do two separate snippets?

Yes I would like to be able to add it to two different packages. Yevhen, How do we add them?

Please try this PHP snippet. Please change 1,2,3 on membership plans id

add_filter( 
	'hivepress/v1/menus/listing_manage/items', 
	function ($items, $menu){
		if ( isset( $items['listing_edit'] ) && hivepress()->get_version( 'memberships' ) ) {
			
			$listing = $menu->get_context( 'listing' );

			if ( hivepress()->helper->is_class_instance( $listing, '\HivePress\Models\Listing' ) && $listing->get_status() === 'publish' ) {
				if(is_user_logged_in() && $listing){
					$membership = \HivePress\Models\Membership::query()->filter(['user' => get_current_user_id()])->get_first();
			
					if(!$membership || in_array($membership->get_plan__id(), [1,2,3])){
						unset($items['listing_statistics']);
					}
				}
			}
		}

		return $items;
	}, 
	1000, 
	2 
);
2 Likes

Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.