1

I want to hide offline payment method, if some particular product is added to cart. For those special products payment should be online, So I need to hide all the offline payment method in that case.

How can I hide payment method based on condition?

  • payment methods that are used to received payment after delivery. Like COD, Cheque etc – Prasanta Hatui Jan 25 '17 at 7:10
2

At last I have found the way to hide all Offline payment methods based on product in cart Please follow the below step:

Step1 : Create a "Yes/No" product attribute with code "is_offline_payment_available" and assigned to the attribute set. This attribute is used to define that the "Offline" payment option is available or not. If it is set "Yes" "Offline" available else not available;

Step2 : Create an observer in app\code\local\YourPackage\YourModule\etc\config.xml with the below code.

<frontend>
    <events>
     <payment_method_is_active>
            <observers>
                <yourmodule>
                    <type>singleton</type>
                    <class>YourPackage_YourModule_Model_Observer</class>
                    <method>hideOfflinePayment</method>
                </yourmodule>
            </observers>
        </payment_method_is_active>
      </events>
</frontend>

Step3 :Create the observer class and method in app\code\local\YourPackage\YourModule\Model\Observer.php with the following code.

<?php

class YourPackage_YourModule_Model_Observer
{

  public function hideOfflinePayment($observer){
    $instance = $observer->getMethodInstance();
    $result = $observer->getResult();
    $allPaymentMethods = $config = Mage::getConfig()->getNode('default/payment')->asArray();
    $allOfflinePayments=array();
    foreach($allPaymentMethods as $code=>$value) {
        if($value['group']=='offline'){
          $allOfflinePayments[]=$code;
        }
    }
    if (in_array($instance->getCode(), $allOfflinePayments)) {
            $cart = Mage::getSingleton('checkout/session')->getQuote();
            $flag=1;
            foreach ($cart->getAllItems() as $item) {
                $_product=Mage::getModel('catalog/product')->load($item->getProductId());
                if($_product->getIsOfflinePaymentAvailable()!=1){
                    $flag=0;
                    break;
                }
            }
            if($flag==1){
                $result->isAvailable = true;
            }
            else{
               $result->isAvailable = false;
            }
    }
  }

}
  • You can accept your own answer, too ;) – sv3n Oct 19 '17 at 7:08
2

+1. Just an improvement to your answer ....

$cart = Mage::getSingleton('checkout/session')->getQuote();

Can be replaced ...

$cart = $observer->getQuote();

And for performance ...

$_product = Mage::getModel('catalog/product')->load($item->getProductId());

Should be replaced with ...

$_product = $item->getProduct();

You have to add this to your etc/config.xml to access your custom attribute in quote item

<global>
   ...
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <is_offline_payment_available/>
                </product_attributes>
            </item>
        </quote>
    </sales>
   ...
</global>

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.