Filtering a Payment Method in Magento
When Magento is checking to see if a payment method is “available” it dispatches an event called
payment_method_is_active passing in a StdClass that holds a local variable called isAvailable.
We only need to change this variable in our own custom logic to affect wether or not the method is available.
Here is how.
In your config.xml (assuming you already have a module set up)
...
<frontend>
...
<payment_method_is_active>
<observers>
<filter_method>
<type>singleton</type>
<class>[vendor]_[modulename]/observer</class>
<method>filterPaymentMethod</method>
</filter_method>
</observers>
</payment_method_is_active>
...
</frontend>
...In Model/Observer.php
<?php
class [Vendor]_[ModuleName]_Model_Observer
{
public function filterPaymentMethod($observer)
{
$method = $observer->getEvent()->getMethodInstance();
if ($method->getCode() === 'purchaseorder') {
$result = $observer->getEvent()->getResult();
// rediculousness
// replace with goodness
if (rand(1,10) == 5) {
$result->isAvailable = false;
}
}
}
}Of course if (rand(1,10) == 5) { is absurd and only there to highlight how easy it is to affect the
availability of a payment method.
Like this post? Share it :)