Support

Home Forums Event Espresso Premium Dynamically changing ability to buy tickets

Dynamically changing ability to buy tickets

Posted: December 20, 2016 at 10:29 pm

Viewing 14 reply threads


westgate.community

December 20, 2016 at 10:29 pm

I need to change the ability to buy tickets or not based on certain criteria about the user – this part i’ve done using PHP in the Event post and I’m able to set a value based on this. So – if i have a Boolean variable like $is_active_user and if it’s false, I want to eliminate the ticket section, how might i go about that? Easy to do with CSS, but not dynamically. Any ideas?


Tony

  • Support Staff

December 21, 2016 at 4:45 am

Hi there,

Are you looking to remove specific tickets or the whole ticket selector?


westgate.community

December 21, 2016 at 7:27 am

Just want to prevent the user from continuing with the purchase somehow, not too fussed… even just hiding the purchase button would be fine. Hiding the whole ticket selector would be fine.


thomaspointbeach

December 21, 2016 at 8:34 am

You have a number of options available to you. The most secure would be to add a hook for the FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data filter, and if you notice a incorrect purchase, clear the total_tickets index and zero-out the qty array (set all or some to zero).

I am going to be submitting a PR here shortly that will add an official filter FHEE__EED_Ticket_Selector__process_ticket_selections__validate_requested_tickets that will behave a bit differently. The valid_post_data filter should be used for modding the post data, where as validate_requested_tickets should be used for go/no-go testing which seems to be what you want.

As for modding the front-end (not a singular solution, as you should *never* trust user-space), it seems you can hook the FHEE__ticket_selector_chart_template__do_ticket_entire_row filter and return any response that is not strictly equal to false (WARNING: NOT returning returns void which is strictly not false, and will result in no tickets showing up).

I wish there was a better filter, perhaps one that hooked in to EE_Ticket::ticket_status() and would allow you to modify the ticket status, maybe to EE_Ticket::archived or a new attr like EE_Ticket::disabled. I may consider adding that as well…

Hope this helps you out…


Josh

  • Support Staff

December 21, 2016 at 9:34 am

@westgate.community, Another approach would be to remove the submit button. Some example code follows:

add_action( 'AHEE__ticket_selector_chart__template__before_ticket_selector', 'my_custom_ts_button_toggle' );
function my_custom_ts_button_toggle( $event ) {
    // you have access to the $event object and with that you can add your criteria logic here
    $is_active_user = false; 
    if ( ! $is_active_user ) {
        add_filter('FHEE__EE_Ticket_Selector__display_ticket_selector_submit', '__return_false', 11 );
    }
}


thomaspointbeach

December 21, 2016 at 10:24 am

Oh, my bad. I miss-read this to say “disable some tickets”. Didn’t understand “all”.

Still, it may also be good to hook FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data to prevent checkout as well. You may also want to not show any tickets if the user is not checked it, in which case you may want to hook something else…


westgate.community

December 21, 2016 at 10:31 am

Thanks for all this. I’d prefer to not allow them to submit at all. Removing the submit button may be the simplest. Not too concerned about security, more about causing confusion. Without teaching me everything that I should already know…. It looks like i need to add a function (to which file?). Will every event then have to have the $is_active_user variable set?


Josh

  • Support Staff

December 21, 2016 at 10:47 am

So you can add your code to a functions plugin or into your WordPress theme’s functions.php file.

It should be possible to write the function so that every event will not need to have the $is_active_user variable set. May I ask are you setting the values for events using the post meta or something like that?


westgate.community

December 21, 2016 at 11:06 am

I’m using S2 Member to validate that a user can access the event at all. The nuance I’m adding (using PHP in the post itself) is that we want to check that their membership doesn’t expire between the time they book the event, to when the event actually happens. I’m doing some simple math in PHP within the Event Post to check their expiry data against the event start date using espresso_event_date_obj() I’ve never used Post Meta much, so open to suggestions.


Josh

  • Support Staff

December 21, 2016 at 11:20 am

Thanks for the clarification. I don’t think storing info in post meta would be part of the solution in this case. So you can probably move the PHP where you check their membership from the post itself into the custom function, you have access to the event object with that hook.


westgate.community

December 21, 2016 at 11:25 am

OK. I don’t want to check membership for every event this way, so I’ll probably just leave it in the event itself for now. I’ll give this a shot a bit later. I’m a bit of a hack, so I appreciate your patience.


westgate.community

December 23, 2016 at 12:07 pm

Josh,

This worked really well. I took your advice and pushed all my code into the function – certainly see the advantage.

Two things are happening now:
1. I’m getting my “message” to unqualified buyers showing up in the post body AND just about the ticket selector; and
2. I’d love to remove the ticket selector table altogether (this code removes the “Register” button, and I know you mentioned hooking something else, but I can’t really sort that out…

This is what I’m using now. I know it’s messy… 😉

add_action( 'AHEE__ticket_selector_chart__template__before_ticket_selector', 'my_custom_ts_button_toggle' );
function my_custom_ts_button_toggle( $event ) {
    // you have access to the $event object and with that you can add your criteria logic here
$timestamp=get_user_field ("s2member_auto_eot_time");
$user_ID = get_current_user_id(); 
$current_event_ID = get_the_ID();
$_event_date_obj = espresso_event_date_obj();
$x = strtotime($_event_date_obj->start_date_and_time());
$diff = $x-$timestamp;
$is_active_user = true; 

echo "<p>";
echo S2MEMBER_CURRENT_USER_DISPLAY_NAME;
echo " | <b>";
echo S2MEMBER_CURRENT_USER_ACCESS_LABEL;
echo "</b>";
echo "<br>";
if (S2MEMBER_CURRENT_USER_ACCESS_LEVEL >=3){ 
    echo "Membership expires: <b>Never</b>";
    echo "<br>Member ID: <b>$user_ID</b>";
} else if (S2MEMBER_CURRENT_USER_ACCESS_LEVEL <3){ 
    echo "Membership expires: <b>";
    echo gmdate("F j, Y", $timestamp);
    echo "</b><br>";
    echo "Member ID: <b>$user_ID";
    echo "</b>";}

if ($diff > 0 and $timestamp > 0){
    $is_active_user = false; 
    echo "<p><h1>It looks like your membership expires between now and the start date of this event.  Please renew your membership before registering.</h1>";
    echo "<h3 align='center'><a href='http://xxxxx.ca/membership-options/purchase-community-memberships/'>- - Renew here before you register! - - </a></h3>";

	}  
    
    if ( ! $is_active_user ) {
        add_filter('FHEE__EE_Ticket_Selector__display_ticket_selector_submit', '__return_false', 11 );
    }
} 


westgate.community

December 24, 2016 at 12:42 am

OK. Making progress. I think i got the above sorted, but now realizing the add_action is causing the test to be run on every event. I only want to run the test when I call the function. How can i make that possible? I’m looking into adding the S2Member properties of the event itself to the conditions so if the page is restricted, we run the test…. Other ideas – I assume we can’t run add_action inside a function that we only call for some events?


westgate.community

December 24, 2016 at 1:56 am

OK nailed this. Merry Christmas. My last request is how to remove the whole ticket selector instead of just the register button….


Josh

  • Support Staff

December 27, 2016 at 11:27 am

One way to remove the whole ticket selector is you add your code onto the
AHEE__EED_Event_Single__use_filterable_display_order__after_add_filters
action hook. It’s important to know that the above hook only fires if the Use Custom Display Order? option for Single Event Pages is set to Yes. So before you try changing the code, you’ll need to head over to Event Espresso > Events > Templates and change that setting if it’s currently set to No.

Then you change the hook from
AHEE__ticket_selector_chart__template__before_ticket_selector
to
AHEE__EED_Event_Single__use_filterable_display_order__after_add_filters.

Since the above hook does not have access to the $event object, you remove the $event parameter from your function, i.e.:
function my_custom_ts_button_toggle() {

Then in that last part where you have this:
add_filter('FHEE__EE_Ticket_Selector__display_ticket_selector_submit', '__return_false', 11 );
You change to:
remove_filter( 'the_content', array( 'EED_Event_Single', 'event_tickets' ), 120, 1 );

Viewing 14 reply threads

The support post ‘Dynamically changing ability to buy tickets’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso