Dynamically Enable/Disable Payment Method in Magento

1 comment
In my recent Magento project I faced a situation in which the Payment methods needed to be enabled/disabled dynamically based on the some conditions. I googled a bit around I found some interesting solutions. I am wrapping up the solution in this article. Magento provides the isAvailable() method in the model of the each payment method. This method tell Magento if the corresponding payment method is available or not for checkout.
Suppose we want to do it for 'PurchaseOrder' payment method. Then go to the model file 'app\code\core\Mage\Payment\Model\Method\Purchaseorder.php'. But the isAvailable() method is not present there. It is generally available for other payment methods. So we will rewrite the model file in our custom module.

Step 1:

Open the config.xml in your custom module do following changes:
<config>
 <global>
  <!-- Rewrite the Purchase order model -->
  <payment>
   <rewrite>
    <method_purchaseorder>Myproject_Mymodule_Model_Method_Purchaseorder</method_purchaseorder>
   </rewrite>
  </payment>
 </global>
</config>

Step 2:

Create the new Model file in your custom module at the location: app\code\local\Myproject\Mymodule\Model\Method\Purchaseorder.php. In this file create the isAvailable() method as:
<?php
class Myproject_Mymodule_Model_Method_Purchaseorder extends Mage_Payment_Model_Method_Purchaseorder
{
 /**
  * Check if payment method is available for use
  * 
  * @param type $quote
  * @return boolean
  */
 public function isAvailable($quote = null) {
  //Here write your logic for enabling/disabling the method
  //if this
   return true;
  //else
   rerturn false;
 }
}
And this is all. All you custom logic goes in the isAvailable() methos for enabling/disabling the payment method.

1 comments:

I constantly spent my half an hour to read this website's articles all the
time along with a cup of coffee.

Also visit my blog post - test

We would love to hear from you...

back to top