Support

Home Forums Event Espresso Premium Hiding payment method(s) for certain user roles

Hiding payment method(s) for certain user roles

Posted: August 1, 2019 at 10:18 am


Tudor

August 1, 2019 at 10:18 am

Hi
When using another events plugin we previously setup additional user roles with enhanced capability (called ‘subscriber+’) in order for these users to pay by invoice in addition to SagePay. How can we restrict invoice payment option to just this user role in EE? I want it to apply to all events across the board.

Thanks


Tony

  • Support Staff

August 1, 2019 at 1:51 pm

Hi there,

You can use a function like this one:

https://gist.github.com/Pebblo/f952f806aec55df5f8a1

Note that on line 10 it checks for the manage_options cap, you’d simply change that to a capability on your role as right now thats intended to add an Invoice payment specifically for admins to use.


jdbell1994

August 6, 2019 at 5:31 pm

Just to clarify what Tudor was asking about since I have a similar need. I would like users who are registered with my “company” usergroup to only have the option to pay via invoice or credit card and then users who have registered as “individuals” to pay by credit card only. How might I manipulate the code you linked to in order to make that happen?

I would assume I would change the code on line 10…
if( is_user_logged_in() && !is_admin() && current_user_can( 'manage_options' ) )

to the following for the individuals…
if( is_user_logged_in() && !is_individual() && current_user_can( 'stripe' ) )
and that would make stripe the only payment method available for individuals.

For companies I would guess it might look something like this…
if( is_user_logged_in() && !is_company() && current_user_can( 'invoice','stripe' ) )

Please let me know if I am on the correct line of reasoning or if I am incorrect. Thank you for the help in advance.


Tony

  • Support Staff

August 7, 2019 at 3:45 am

The above is incorrect.

is_admin() is not checking if the user is an admin, its checking if the code is loading in the dashboard (the WP Admin), see HERE.

So is_individual() and is_company() do not exist and they’ll throw a fatal error if you try to use those (unless they are custom functions you’ve created, but I’m assuming not).

current_user_can( 'manage_options' ) is checking for a ‘manage_options’ capability on the current user, admins have that capability meaning it’s checking if the user is an administrator (technically any user can have that capability but its generally only admins).

The capability check does not affect the payment methods themselves, it’s just a conditional for you to load the code that will add/remove payment methods so current_user_can( 'invoice','stripe' ) is not going to work how you are expecting.

The filter is passed an array of payment method objects that would normally load on every event, so you’ll have both Invoice and Stripe enabled, meaning they will be within that array but the tough part of this is the array will use the payment method IDs as the keys of the array which means the array will be unique for each site.

What I would do is move the capability check from Line 10 down inside the EE_Transaction condition and filter the $payment_methods array there based on the user’s capability.


jdbell1994

August 7, 2019 at 11:12 am

Thank you for the response Tony! My apologies for not understanding.
So you are saying I would need the payment method IDs, do you know where I could find that information? You also said moving the capability check to inside the EE_Transaction Condition and filter the $payment_methods array there based on user capability. Could elaborate on that a little more or provide an example, I’m a little stuck. Thank you.


jdbell1994

August 7, 2019 at 12:19 pm

My need is very similar to Tudor’s, I just want the members of the company usergroup to be able to use the invoice payment method. I don’t understand what changes to make to the function you supplied to make that happen. Based on your first response it sounded like on line 10 I should change the “manageoptions” cap but I am not sure what it should be changed to or how this function knows what usergroup to look for.


Tony

  • Support Staff

August 7, 2019 at 3:39 pm

Your need is very different from Tudors.

From Tudor’s opening post:

When using another events plugin we previously setup additional user roles with enhanced capability (called ‘subscriber+’) in order for these users to pay by invoice in addition to SagePay.

So they need to add the ability to select Invoice for users with a specific capability whilst leaving the current payment method available, which in short was simply a case of changing that capability check as it already adds an invoice payment method if your an admin, so that just changes ‘if your an admin’ check to a different capability and injects an additional payment method.

You need to set specific payment methods based on the user’s capability, that’s very different to Tudors request and you need to customize the function I provided as an example, it’s not a case of simply swapping out that capability as it was for Tudor.

You also said moving the capability check to inside the EE_Transaction Condition and filter the $payment_methods array there based on user capability. Could elaborate on that a little more or provide an example, I’m a little stuck.

Remove the capability check from line 10, so it would just be:

if( is_user_logged_in() && !is_admin() )

Then within the EE_Transaction check, which is this be:

if ( $transaction instanceof EE_Transaction) {
    // some code
}

Check the user capability and add/remove the payment methods you need from there.

So inside that, you may have something like:

if( current_user_can( 'company' ) ) {
    // This code runs to 'company' users.
    //Pull in an Invoice payment method.
    $invoice_payment_method = EEM_Payment_Method::instance()->get_one_of_type( 'Invoice' );
    //Check we actually have an EE_Payment_Method object and add it to the payment methods.
    if( $invoice_payment_method instanceof EE_Payment_Method ) {
        $payment_methods = array();
        $payment_methods[$invoice_payment_method->ID()] = $invoice_payment_method;
    }

} elseif ( current_user_can( 'individuals' ) ) {
    // This code runs for 'individual'
    //Pull in an Invoice payment method.
    $invoice_payment_method = EEM_Payment_Method::instance()->get_one_of_type( 'Invoice' );
    //Check we actually have an EE_Payment_Method object and add it to the payment methods.
    if( $invoice_payment_method instanceof EE_Payment_Method ) {
        // Remove the Invoice payment method from the available payment methods
        unset($payment_methods[$invoice_payment_method->ID()]);
    }
}

As you can see, because you want to show/hide payment method based on the user its not a simple as swapping out a conditional.

The support post ‘Hiding payment method(s) for certain user roles’ 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