Displaying different gateways for different events

The 1.5 update added the filter eventorganiser_booking_form_gateways, this allows you to filter the gateways that appear on the booking form (and also to do so by event).

Lets suppose you have multiple events and want to accept offline payments on some, and PayPal on others. You can do that from 1.5 with a bit of code.

In the following example, it’s expected that each event has a custom field with key ‘gateways’. This custom field key has each gateway to be shown for the event as separate values. If this key doesn’t exist for an event, all enabled gateways are used. Otherwise gateways are only shown if they are both enabled and listed by the event.

Here’s the snippet (with comments) on how to achieve that:

add_filter( 'eventorganiser_booking_form_gateways', 'my_booking_form_gateways', 10, 3 );
function my_booking_form_gateways( $enabled_gateways, $booking_form_id, $booking_form ){

    //Get event ID
    $event_id = $booking_form->get('event_id');

    //Get the gateways listed for this event
    $gateways = get_post_meta( $event_id, 'gateways' );

    //If we can't find any gateways stored with this event - abort
    if( !$gateways )
         return $enabled_gateways;

    //Get only gateways which are both enabled and specified for this event
    $enabled_gateways = array_intersect_key( $enabled_gateways, array_flip( $gateways ) );

    return $enabled_gateways;