2

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.

3
  • I don't recall the structure of a rate result off the top of my head, but it may be as simple as using $result->setData('is_available',false) instead of setting that direct variable, since I think it's a varien object. The variable might also just be available inside the object, with isAvailable() as the custom accessor. Commented May 29, 2013 at 20:26
  • my problem is actually that "if condition" for shipping method now working correctly. Because for all shipping method that is activate result is true and show just 2 payment methods that I want. So I must have condition that check which shipping method is chosen during checkout.
    – srdan
    Commented May 29, 2013 at 20:48
  • R.S could you please add the config.xml file for this module? Commented Sep 3, 2015 at 10:39

3 Answers 3

2
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;
        }
    }
2

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

             }
}
1

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

1
  • ok thanks for advice. Could you please describe me how to get in variable which shipping method user choose, so that use it in my if condition for shipping method.
    – srdan
    Commented May 30, 2013 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.