As a developer or, a store owner, we face the mentioned situation very often.
How to filter payment methods based on some conditions applicable for my magento store?
There are many of ways of creating it;
- By creating an observer for the event payment_method_is_active. Which is in fact the best technique to use.
- By overriding your theme’s default template files: app/design/frontend/[PACKAGE]/[THEME]/template/checkout/onepage/payment/methods.phtml
- By overriding method: Mage_Payment_Model_Method_Abstract::isAvailable()
- By overriding method: Mage_Checkout_Block_Onepage_Payment_Methods::_canUseMethod()
- By overriding method: Mage_Checkout_Block_Onepage_Payment_Methods::getMethods()
Among all the above methods, I’m discussing the first technique, i.e. creating an observer for the event payment_method_is_active.
Case Study: I have a magento store with two currencies, i.e. INR and USD, so, I would like to filter payment methods based on the currency customer has chosen. I will show only Saved CC Payment method for customers using USD as their currency.
Create a Module in app/code/local/Insync/ Paymentfilter
Register the event: payment_method_is_active in config.xml.
Add the following xml code in app/code/local/Insync/Paymentfilter/etc/config.xml
<!--?xml version="1.0"?--> 1.0 singleton Insync_Paymentfilter_Model_Observer paymentMethodIsActive
Now, we need to create the observer file, where actual logic will be implemented.
Create observer file: app/code/local/Insync/Paymentfilter/Model/Observer.php and paste the following code:
Step 4 Now in this final step, enable & register your module with magento. Create the module xml file: app/etc/modules/Insync_Paymentfilter.xml and paste the following code:
<!--?xml version="1.0"?--> <!--?xml version="1.0"?--> true local 0.1.0