Creating the Difference

Set Gateway for Logged-in Users Only

Posted on: 03-10-2018 Last modified: 03-07-2021
Categories: PHP, Snippets Tags: , ,

In Events Manager Pro you can activate several payment gateways. It ships with PayPal (Payments Standard), AIM and and Offline.
There are several third-party gateways available in the WordPress repository, like my own EM – Mollie Gateway for iDEAL.
You can activate them in Events -> Payment Gateways.

With my plugin EM Gateway Selector you can now easily decide which gateway should or should not be available for bookings per event.
But what if we took it even a step further?

Only show specific gateways if user is logged in.

Let’s say you sell tickets to non-users (“Allow guest bookings, yes”), but also have loyal members with their own login. You could choose to have non-registered visitors only book tickets with a direct, online payment, but your loyal members are allowed to pay at the door (offline payment).
In that case, you would want to activate that gateway only for logged-in users.

Events Manager works just the other way around in this case. Technically that is very logical:
You have to activate the gateway (for all) in order for Events Manager to technically initiate all settings, etc. Then, for the guest bookings, you would have to deactivate (unset) the specific gateway.

First, make sure that the gateway is activated in your Events Manager settings. Then add this to your functions.php:Click here to learn how to safely add your code to WordPress.

[php] function stonehenge_em_gateway_for_users() { if( !is_user_logged_in() ) { global $EM_Gateways; // Specify each gateway by its slug. unset( $EM_Gateways[‘offline’] ); echo ” “; } } add_action( ’em_booking_form_footer’, ‘stonehenge_em_gateway_for_users’, 9 ); [/php]

That’s it! Now, your logged-in users will see the offline payment in the gateway dropdown, but guests will not.

Use Gateways based on User Role?

Of course you can extend this little trick a bit further.
Let’s say you only want to allow Admins to add offline payments; maybe just as a courtesy or if you have to move a booking from one event to another. Or perhaps because the offline gateway is one of the few that acutally allows manual bookings from the admin back-end.

[php] function stonehenge_em_gateway_for_admins() { $user = wp_get_current_user(); if ( in_array( ‘administrator’, (array) $user->roles ) ) { global $EM_Gateways; // Specify each gateway by its slug. unset( $EM_Gateways[‘offline’] ); echo ” “; } } add_action( ’em_booking_form_footer’, ‘stonehenge_em_gateway_for_admins’, 9 ); [/php]
⇐ Previous
Next ⇒