7

I'm using CE 1.9.0.1 and have correctly created and registered my module, but I can't seem to detect the shipping methods. Basically, I want to hide Cash on Delivery if Flat Rate or Free Shipping is chosen. Here is the code I have for my observer class:

class Kol_PaymentToggle_Model_Observer
{
    public function paymentMethodIsActive(Varien_Event_Observer $observer) {
        $event  = $observer->getEvent();
        $method = $event->getMethodInstance();
        $result = $event->getResult(); 
        $quote = $observer->getEvent()->getQuote();
        $shippingMethod = $quote->getShippingAddress()->getShippingMethod();
        if($shippingMethod == "standardshipping" || $shippingMethod == "free") {
            if($method->getCode() == 'cashondelivery' ) {
                $result->isAvailable = false;
            }
        }
    }
}

I'm guessing that I haven't used the correct shipping method code names or payment method code names, but I'm unsure. Anyone has any advice?

EDIT: I only have 3 shipping methods enabled:

  • Collect In Store
    Title = Collect in Store
    Method Name = Collect In Store (Extension link)
  • Flat Rate
    Title = Standard Delivery
    Method Name = Standard Shipping
  • Free Shipping
    Title = Free Delivery
    Method Name = Free

Hopefully this extra information can prove useful towards helping me!

7

I got an answer to this same question on StackOverflow by user Elvarasan. His answer provided me with everything I needed to get this working.

To hide some payment methods based on shipping method you don't need to observe things at all:

Every methods(in one page check out) post the methods which are chosen to the next level. So you can get the shipping method which are chosen, in payment method level. Just print the post things in

app/design/frontend/base/default/template/checkout/onepage/payment/methods.phtml

in this add below one,

<?php print_r($_POST); ?>

So now you can get the shipping methods which are chosen previous step. And note it, so now, you can add just simple logic (if else) condition in same file for hiding payment.

For example here I want hide check / money order payment method, if shipping method is flat. Here the payment method code is checkmo. you can get payment method code by simply printing that variable like echo $_code = $_method->getCode(); in same file. so here just add simple if else ,

<?php
    $methods = $this->getMethods();
    $oneMethod = count($methods) <= 1;
?>
<?php if (empty($methods)): ?>
    <dt>
        <?php echo $this->__('No Payment Methods') ?>
    </dt>
<?php else:
    foreach ($methods as $_method):
       echo  $_code = $_method->getCode();


if($_POST['shipping_method'] == 'flatrate_flatrate') {
    if($_code == 'checkmo') {
        continue;
    }
}
?>

Here,

if($_POST['shipping_method'] == 'flatrate_flatrate') {
    if($_code == 'checkmo') {
        continue;
    }
}

checks the shipping method and skip the payment method which we don't want to display. That's it. Please comment here if you have any doubt.

Note:

 shipping_method => flatrate_flatrate
 payment_method   => checkmo
  • If i have Owebia Shipping for shipping methods and IWD One Page Checkout, is it the right method to apply? Thanks – Simbus82 May 30 '16 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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