How to filter payment method in onepage checkout

January 9, 2013  |  4 Comments  |  by MagePsycho  |  Latest, Magento

Introduction

Q: How will you filter the payment method in onepage checkout based on some conditions?
A: There are different ways to do so. Some of them are:
#1 By overriding template: app/design/frontend/[interface]/[theme]/template/checkout/onepage/payment/methods.phtml
#2 By overriding method: Mage_Checkout_Block_Onepage_Payment_Methods::_canUseMethod()
#3 By overriding method: Mage_Payment_Model_Method_Abstract::isAvailable()
#4 By overriding method: Mage_Checkout_Block_Onepage_Payment_Methods::getMethods()
#5 By observing event: payment_method_is_active
#6 etc.

Among above methods obviously using event-observer technique is the best way to go (#5).
And here I will be discussing about how to enable the PayPal (Website Standard) method only when current currency is USD.

Steps

Suppose a skeleton module(MagePsycho_Paymentfilter) has already been created.
1> Register the event: ‘payment_method_is_active’ in config.xml.
Add the following xml code in app/code/local/MagePsycho/Paymentfilter/etc/config.xml:

...
<frontend>
	...
	<events>
		<payment_method_is_active>
			<observers>
				<paymentfilter_payment_method_is_active>
					<type>singleton</type>
					<class>paymentfilter/observer</class>
					<method>paymentMethodIsActive</method>
				</paymentfilter_payment_method_is_active>
			</observers>
		</payment_method_is_active>
	</events>
	...
</frontend>
...

2> Implement the observer model
Create observer file: app/code/local/MagePsycho/Paymentfilter/Model/Observer.php and paste the following code:

<?php
/**
 * @category   MagePsycho
 * @package    MagePsycho_Paymentfilter
 * @author     magepsycho@gmail.com
 * @website    http://www.magepsycho.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 	*/
class MagePsycho_Paymentfilter_Model_Observer {

	public function paymentMethodIsActive(Varien_Event_Observer $observer) {
		$event			 = $observer->getEvent();
		$method			 = $event->getMethodInstance();
		$result			 = $event->getResult();
		$currencyCode	 = Mage::app()->getStore()->getCurrentCurrencyCode();

		if( $currencyCode == 'USD'){
			if($method->getCode() == 'paypal_standard' ){
				$result->isAvailable = true;
			}else{
				$result->isAvailable = false;
			}
		}
	}

}

3> Go ahead for testing.

Happy E-Commerce!

Posted in Latest, Magento and tagged , , , , . Bookmark the permalink.

About MagePsycho

Zend PHP5 Certified Engineer, Magento Certified Developer, Magento Moderator / Freelancer with specialization in web applications (CMS, E-Commerce, ERP etc.). Twitter: @magepsycho Skype: @magentopycho

4 Responses to How to filter payment method in onepage checkout

  1. Implementation of this observer method affects the frontend and admin pages of Magento, correct? Not just within One Page Checkout.

    In order to have it only affect one or the other,would you change the config xml to fall within the “frontend” or “admin” tags, rather than the “global” tag?

    We want all payment methods available for use by our phone sales reps, but only certain methods for online orders made by customers on the frontend.

  2. Can this be also implemented so that payment methods are shown based on Zipcode entered by users..

  3. khushbu

    i am not getting enabled cash on delivery method can i have zip file of code plz.
    thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>