OnePage Checkout - Hide Payment method depending on Shipping Method
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 isflat
. Here the payment method code ischeckmo
. you can get payment method code by simply printing that variable likeecho $_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