Magento – How to disable purchase order for guest checkout

magento-1.9payment-methodsrwd

In magento 1.9.2.4 you can disable checkout for guests completely. However, what I would like is if customer chooses to checkout as a guest then they can only checkout via paypal standard. But, if they choose to register and checkout then they have the option of being able to checkout with paypal or purchase order.

Would this even be possible? I am using a theme based on the default RWD theme.

Best Answer

I'd suggest using an observer on the payment_method_is_active event. This fires just before Magento renders the payment methods in the checkout, enabling you to step in and switch methods on and off as you please.

<config>
<global>
    <events>
        <payment_method_is_active>
            <observers>
                <disable_purchase_orders_for_guests>
                    <class>VendorName_ModuleName_Model_Observer</class>
                    <method>disablePurchaseOrders</method>
                </disable_purchase_orders_for_guests>
            </observers>
        </payment_method_is_active>
    </events>
</global>

In this observer you'd need to check the payment method instance against the payment method you want in this case "purchaseorder"

public function disablePurchaseOrders(Varien_Event_Observer $observer) 
{
  $paymentMethodCode = $observer->getEvent()->getMethodInstance()->getCode();
  if ($paymentMethodCode === 'purchaseorder') { 

Also check if the customer is a guest

    if (!Mage::getSingleton('customer/session')->isLoggedIn()) {

Then disable the payment method

       $result = $observer->getEvent()->getResult();
       if ($result->isAvailable) {
        $result->isAvailable = false;
      }
    }
  }
}

Here's a module I wrote that disables payment methods based on delivery address. It's a little more complicated than what you need as it sets admin fields to enable/disable for each payment method and allow users to select which contries to do this for. Hopefully it will get you started. https://github.com/peacockcarter/magento-filter-payment-methods-on-shipping-address

Related Topic