0

Here's the requirement. Magento 1.9.3+ Customers can have certain disabled payment methods enabled if a customer attribute is set. So, we have a customer attribute payment_method, which is a dropdown. We have disabled certain payment methods - like checkmo, purchaseorder.

I've written an observer that will enable a disabled payment method for logged in customers. However, guest or not logged in customers are greeted with the popup message after completing the 1st step in the one page checkout

"Customer Payment Method" is a required value.

Here is my config.xml where I declare my method:

<frontend>
    <events>
         <payment_method_is_active>
            <observers>
                <fws_paymentmethod_save_observer>
                    <type>singleton</type>
                    <class>paymentmethod/Observer</class>
                    <method>payment_method_is_active</method>
                </fws_paymentmethod_save_observer>
            </observers>
         </payment_method_is_active>
    </events>
</frontend>

My observer is pretty simple:

class Fws_PaymentMethod_Model_Observer{

/**
 * Match disabled payment methods to methods allowed to customer via the payment method dropdown attribute value
 * if customer is allowed to use a disabled method, then enable that method.
 * Defaults are paypal_express, sagepaydirectpro.
 * @param $observer
 *
 */
public function payment_method_is_active(Varien_Event_Observer $observer)
{

        $method          = $observer->getMethodInstance();
        $result = $observer->getResult();
        if(Mage::getSingleton('customer/session')->isLoggedIn()){

        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $_attr = $customer->getResource()->getAttribute('payment_method')->getFrontend()->getValue($customer);
            Mage::log(" logged in Payment method: " .$_attr);

                try{
                    if($_attr == "PO Account"){
                        if($method->getCode() == "purchaseorder" ){
                            $result->isAvailable = true;
                            $result->save;
                            Mage::log(" logged in Payment method PO shd be set to true : " .$_attr );
                        }
                    }
                    elseif ($_attr == "BACS"){
                        if($method->getCode()=='banktransfer'){
                            $result->isAvailable = true;
                            $result->save;
                            Mage::log(" logged in Payment method BT shd be set to true : " .$_attr);
                        }
                    }
                    elseif($_attr == "Cheque"){
                        if($method->getCode() == "checkmo" ){
                            Mage::log(" logged in Payment method Checkmo shd be set to true : " .$_attr);
                            $result->isAvailable = true;
                            $result->save;
                        }
                    }
                }
                catch( Exception $e ) {
                    Mage::log( "payment_method_is_active observer failed: " . $e->getMessage() );
                }

        }else{
                Mage::log(" NOT logged in  " );
                try{

                    if($method->getCode() == "paypal_express"){
                        $result->isAvailable = true;
                        $result->save;
                        Mage::log(" NOT logged in  setting paypal express to true" );
                    }
                    if($method->getCode() =="sagepaydirectpro"){
                        $result->isAvailable = true;
                        $result->save;
                        Mage::log(" NOT logged in setting sagepay to true " );
                    }
                }catch( Exception $e ) {
                    Mage::log( "payment_method_is_active observer failed: " . $e->getMessage() );
                }
            }


}

I can't figure out why guests or not logged in users are being presented with this popup during checkout. It seems like all payment methods have been removed or something??

Your Answer

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

Browse other questions tagged or ask your own question.