2

HOW to display/hide "Cash on delivery" payment method for some specific cities.

I have tired this link also but not working

This is my observer.php located in app/code/local/MagePsycho/Paymentfilter/Model

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();

        $checkout = Mage::getSingleton('checkout/session')->getQuote();
        $shipping = $checkout->getShippingAddress();

        $cashOnDeliveryCities = array('test1','test3');

        if(in_array($shipping->getCity(), $cashOnDeliveryCities)){
            $result->isAvailable = true;
        }else{
            $result->isAvailable = false;
        }   

    }

}

This is my config.php located in app/code/local/MagePsycho/Paymentfilter/etc

<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>

Where i am missing, It showing cash on delivery for all cities not filtering

Thanks

Community
  • 1
  • 1
Ram
  • 339
  • 1
  • 3
  • 11

1 Answers1

1

It works for me.

Firstly check, that your observer method is executing. Put something like exit() or die();

As you have already said you'd like to add city restriction only for cash_on_delivery - add following code after $currencyCode = ...

    if(!($method instanceof Mage_Payment_Model_Method_Cashondelivery))
    {
        return;
    }

Try to debug Mage_Payment_Model_Method_Abstract::isAvailable. Put some Mage::log in this method and determine at which moment which walue have $checkResult->isAvailable

zhartaunik
  • 932
  • 12
  • 29