1

I want to show specific payment method when specific shipping is selected

Already Implement this solution but not works for me

code update in

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

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

?>

https://stackoverflow.com/questions/26604267/magento-onepage-checkout-hide-payment-method-depending-on-shipping-method

enter image description here enter image description here

0

The answer in the given link is not a good solution in my point of view. Because it checks post data in a phtml page, which is not at all good approach.

Instead you need to look for any events to do your job. Luckily there is an event for you to do that. The event is: payment_method_is_active.

This event is fired from the method Mage_Payment_Model_Method_Abstract::isAvailable(). This method will be checked by Magento when it prepares the available payment methods for checkout, which makes this event best to avoid certain payment methods as per our need.

Set up an observer which listens to the event payment_method_is_active in your module and your observer should look like this.

public function disablePaymentMethodByShippingMethod(Varien_Event_Observer $observer)
{
    $result = $observer->getResult();
    $methodInstnce = $observer->getMethodInstance();
    $quote = $observer->getQuote();
    $shipMethod = $quote->getShippingAddress()->getShippingMethod();

    //if ship method is flatrate_flatrate, then disable payment method: checkmo.
    if ($shipMethod == 'flatrate_flatrate' && $methodInstnce->getCode() == 'checkmo') {
        $result->isAvailable = false;
    }

    return $this;
}

Here we are just checking the shipping method opted is flatrate_flatrate. If yes, then set $result->isAvailable to false. If this is set to false, then Magento won't add that payment method into available payment methods list.

Hope you get the idea. I didn't test this code. But this is the right way to do this.

  • hello bro can i place this code at \app\code\core\Mage\Payment\Model\Method\Abstract.php ? – Danish Ali Oct 16 '17 at 7:41
  • bro i found this link is it suitable for me webspeaks.in/2013/12/… – Danish Ali Oct 16 '17 at 10:14
  • No you cannot place this code in that file. It is basically become a core edit. You should know how to create a module in magento and how to configure an observer through it, in order to use my answer. – Rajeev K Tomy Oct 16 '17 at 16:05
  • The link is also provides another solution, but still it uses a class rewrite. This solution is more clean and it is the best way to do this work – Rajeev K Tomy Oct 16 '17 at 16:08

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.