Skip to content

Instantly share code, notes, and snippets.

@skyshab
Created June 18, 2021 14:15
Show Gist options
  • Save skyshab/c28ee1f31c88afb809ec00cd602ce544 to your computer and use it in GitHub Desktop.
Save skyshab/c28ee1f31c88afb809ec00cd602ce544 to your computer and use it in GitHub Desktop.
Hide tickets for non members based on user role. Membership plugins. Ultimate member.
<?php
add_filter( 'tribe_template_context', function($context, $file, $name, $obj){
// bail if not the target template
if ( 'v2/tickets' !== implode("/", $name) ) {
return $context;
}
// This is the name of a category added to members only products in WooCommerce.
$members_only_product_category = 'members-only';
// This is the name or id of the required membership level.
$required_membership_role = 'administrator';
// Get current user
$user = wp_get_current_user();
// Can this user view members only tickets?
$can_view_member_tickets = ( in_array( $required_membership_role, (array) $user->roles ) ) ? true : false;
// Check all the tickets
foreach( $context['tickets'] as $index => $ticket ) {
if( ! has_term( $members_only_product_category, 'product_cat', $ticket->ID ) ) continue;
if( ! is_user_logged_in() || ! $can_view_member_tickets ) {
$on_sale_index = array_search($ticket->ID, array_column($context['tickets_on_sale'], 'ID'));
unset( $context['tickets'][$index] );
unset( $context['tickets_on_sale'][$on_sale_index] );
}
}
return $context;
}, 100, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment