Monday , December 11 2017
Home >> All tutorial >> How to filter payment method in one page checkout?

How to filter payment method in one page checkout?

There are several way to filter payment method in one page checkout:

  • By overriding template: app/design/frontend/[interface]/[theme]/template/checkout/onepage/payment/methods.phtml
  • By overriding method: Mage_Checkout_Block_Onepage_Payment_Methods::_canUseMethod()
  •  By overriding method: Mage_Payment_Model_Method_Abstract::isAvailable()
  •  By overriding method: Mage_Checkout_Block_Onepage_Payment_Methods::getMethods()
  •  By observing event: payment_method_is_active
Among above methods obviously using event-observer technique is the best way to go.
And here I will be discussing about how to enable the PayPal (Website Standard) method only when current currency is USD.
Suppose a skeleton module(MagePsycho_Paymentfilter) has already been created.
Step 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:
  1. <frontend>
  2.     
  3.     <events>
  4.         <payment_method_is_active>
  5.             <observers>
  6.                 <paymentfilter_payment_method_is
  7. _active>
  8.                     <type>singleton</type>
  9.                     <class>paymentfilter/observer</class>
  10.                     <method>paymentMethodIsActive</method>
  11.                 </paymentfilter_payment_method_is_active>
  12.             </observers>
  13.         </payment_method_is_active>
  14.     </events>
  15.     
  16. </frontend>
Step 2: Implement the observer model
Create observer file: app/code/local/MagePsycho/Paymentfilter/Model/Observer.php and paste the following code:
  1. <?php
  2. /**
  3.  * @category   MagePsycho
  4.  * @package    MagePsycho_Paymentfilter
  5.  * @author     [email protected]
  6.  * @website    http://www.magepsycho.com
  7.  * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  8.     */
  9. class MagePsycho_Paymentfilter_Model_Observer {
  10.  
  11.     public function paymentMethodIsActive(Varien_Event_Observer $observer) {
  12.         $event           = $observer->getEvent();
  13.         $method          = $event->getMethodInstance();
  14.         $result          = $event->getResult();
  15.         $currencyCode    = Mage::app()->getStore()->getCurrentCurrencyCode();
  16.  
  17.         if( $currencyCode == ‘USD’){
  18.             if($method->getCode() == ‘paypal_standard’ ){
  19.                 $result->isAvailable = true;
  20.             }else{
  21.                 $result->isAvailable = false;
  22.             }
  23.         }
  24.     }
  25.  
  26. }
 
Step 3: Go ahead for testing.

We also contribute great content of Magento Download for FREE (all versions)

DON'T MISS THE CHANCE!

magento download

About Champ

Leave a Reply

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

Heap | Mobile and Web Analytics