Created
April 2, 2021 12:42
-
-
Save skyshab/72858eb70ee80d7f443fb6f03c730785 to your computer and use it in GitHub Desktop.
Hide members only tickets from ticket form when using Paid Memberships Pro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Hide members only tickets from ticket form when using Paid Memberships Pro. | |
// Works by assigning a product category and defining the membership needed. | |
add_filter( 'tribe_template_context', function($context, $file, $name, $obj){ | |
// bail if not the target template, or if membership plugin isn't active | |
if ( 'v2/tickets' !== implode("/", $name) || ! function_exists( 'pmpro_hasMembershipLevel' ) ) { | |
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_level_name = 'All access Pass'; | |
foreach( $context['tickets'] as $index => $ticket ) { | |
if( ! has_term( $members_only_product_category, 'product_cat', $ticket->ID ) ) continue; | |
if( ! is_user_logged_in() || ! pmpro_hasMembershipLevel($required_membership_level_name) ) { | |
$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
Thanks but...
The code does not work as expected.
unset( $context['tickets_on_sale'][$on_sale_index] );
seems to be the issue.But if I
unset( $context['tickets_on_sale'][$index] );
then it appears to work.What do you think?