Posted: November 18, 2018 at 1:01 pm
I am trying to filter the shown payments methods based on some user metrics.
I am trying to use the debug tool that Tony mentioned, but I struggle to find the correct hook/filter in the sea of filters. I found these filters/hooks
None of them are for my intentions. Any hints which one might be the correct one and how to proceed?
I also struggle to find out, once I found a hook, which and how many parameters it takes. Any tips on that?
Sometimes, Debug bar doesn’t list all of the hooks used so you need to do a little more within the code itself.
I can tell you, that the hook you want to use is:
I have some examples of using that hook here:
Those snippets add an additional payment method based on various factors.
EE will have already have pulled in your payment methods are that point and pass them to the hook as an array of EE_Payment_Method objects on the first parameter, so you can unset whichever one you don’t want that specific user to see.
The code within the plugin never lies so use the code 🙂
By that I mean we will eventually have some better documentation for all of our hooks, however, things change quickly, so the best place to look is in the code.
Open up Event Espresso in a text editor/IDE (Sublime Text/PHPStorm) and search for the hook, in this case we’ll use the above – http://take.ms/zuqA2
Looking at the docs for apply_filters() it’s:
There are multiple options for additional params, they can be passed within an array or individually, EE usually passes each param individually, as is the case here.
Line 371 shows the filter name.
Line 372 is whatever is actually being filtered (in this case an array of EE_Paymnt_Methods)
Lines 373 – 374 are the additional params used to help with filtering (for example you’ll need the transaction object to know what your filtering for).
Meaning for this filter there are 3 parameters.
Does that help?
Our models usually return an array, so this:
Is an array of payment methods and this is what is ‘filtered’, you must return the first parameter with your chose value(s) in the same format is was passed.
The doc block above the function adds some details on what is returned and what the function does.
Hey, Tony, that was exactly the hook I was looking for. Thanks for the samples. I implemented the needed function on my side and it works.
Back to the proper approach to actually find the correct hook. I do not have any problems getting messy and search through the core code. In fact, I did just that my whole saturday. 😀
That is exactly my point, where or how do I get the name of the hook I am searching for in the first place. Do you just grep search all files in the core plugin dir that contain ‘payment’? Or is there another approach/rule that can help to find the hook I am looking for?
The way I would do it is to log all of the hooks fired on the request and look through those, for that you can use a function like this:
Lots of different ways to do this tbh, it depends on what output you want to use, but point being you log all of the hooks and then search through them to see if one is suitable.
So in this example run a registration and search through the hooks when finished, or better yet, open the payment options, enable the function and then refresh the page, now you’ll get the hook specifically for the current request.
Your filtering payment methods so I’d start with within for ‘payment_method’ and see what was available, then ‘payment’ if nothing stood out and so on. Find anything that looks like it handles payment methods and you have a rough idea on where to look in the code by searching for that hook.
Note, don’t leave that function enabled all of the time, EE fires a LOT of hooks, my function logs them once and its still over 300 hooks without duplicates. Your log file is going to fill up quickly if you leave that enabled.
Tony, that is epic.
So far I have used this process for two more developments already.
Great, I’m glad that helped 🙂
The support post ‘Filter payment methods’ 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.