Question

I develop observer method in Magento for filter payment methods based on shipping methods. This is my method:

class Devpassion_Paymentfilter_Model_Observer {

public function paymentMethodIsActive(Varien_Event_Observer $observer) {
$event           = $observer->getEvent();
$method          = $event->getMethodInstance();
$result          = $event->getResult();
$carriers = Mage::getSingleton('shipping/config')->getActiveCarriers();

    foreach ($carriers as $carrier) {
 //       $carrierCode = $carrier->getId();

        if ($carrier->getId() == 'flatrate' ){
                if($method->getCode() == 'checkmo' OR $method->getCode() == 'paypal_standard'){
                    $result->isAvailable = true;
                }else{
                    $result->isAvailable = false;
                }
            }

         }
    }
}

Results of this is that for all shipping method this filter is true. So for all shipping method paypal and money check shows up and all other not.

Please advice me how to set up this condition to filter just for one specific shipping method.

Was it helpful?

Solution

public function paymentMethodIsActive($observer)
    {
        /**
         * @var $quote Mage_Sales_Model_Quote
         */
        $quote  = $observer->getEvent()->getQuote();
        $method = $observer->getEvent()->getMethodInstance();
        $result = $observer->getEvent()->getResult();

        $shipping_method = $quote->getShippingAddress()->getShippingMethod(); //selected shipping method
        if ($shipping_method == 'flatrate_flatrate' && $method->getCode() == 'checkmo') {
            $result->isAvailable = false;
        }
    }

OTHER TIPS

I'm not 100% person sure on what you are trying to accomplish, but shouldn't you be checking the shipping method against the current order instead of the global list of all carriers.

Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod()

Instead of

$carriers = Mage::getSingleton('shipping/config')->getActiveCarriers();

See https://stackoverflow.com/questions/6032936/how-do-i-get-the-shipping-method-the-user-has-chosen-during-checkout

Thanks to all. Now my method with combination of all answers looks like that and works perfect so hope helps to someone else:

class Devpassion_Paymentfilter_Model_Observer {

public function paymentMethodIsActive(Varien_Event_Observer $observer) {


    $event           = $observer->getEvent();
    $method          = $event->getMethodInstance();
    $result          = $event->getResult();
    $carriers = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod();

            if ($carriers == 'flatrate_flatrate' ){
                    if($method->getCode() == 'checkmo' OR $method->getCode() == 'paypal_standard'){
                        $result->isAvailable = true;
                    }else{
                        $result->isAvailable = false;
                    }
                }

             }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top