更新时间:2023-11-30 10:36:22
一种不显眼的过滤支付方式的方式是为名为 payment_method_is_active
的事件实现观察者步骤
1> 在 config.xml 中注册事件:‘payment_method_is_active’.在 app/code/local/MagePsycho/Paymentfilter/etc/config.xml 中添加以下 xml 代码:
An unobtrusive way of filtering payment method is to implement observer for event called payment_method_is_active
Steps
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:
...
<global>
...
<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>
...
</global>
...
2> 实现观察者模型创建观察者文件:app/code/local/MagePsycho/Paymentfilter/Model/Observer.php 并粘贴以下代码:
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($someTrueConditionGoesHere){
$result->isAvailable = true;
}else{
$result->isAvailable = false;
}
}
}