sulman
sulman

Reputation: 2461

Magento - Dynamically disable payment method based on criteria

I have an extremely simple module that allows a customer to "Purchase On Account". The module doesn't do anything special really (it was simply modified from a Cash On Delivery module.)

The problem is I only want to offer this payment method to logged in customers.

So far my module looks like this:

BuyOnAccount/
    etc/
        config.xml
        system.xml
    Model/
        PaymentMethod.php

The content of PaymentMethod.php is:

class MyCompany_BuyOnAccount_Model_PaymentMethod extends Mage_Payment_Model_Method_Abstract
{
    protected $_code  = 'buyonaccount';
    protected $_isInitializeNeeded      = true;
    protected $_canUseInternal          = false;
    protected $_canUseForMultishipping  = false;
}

The config and system xml files contain the usual sort of thing (please let me know if you would like to see the code and i'll edit)

So bascically I need to disable the module if the user is not logged in (but obviously only for the current customer session!)

Any ideas?

Thanks

Upvotes: 4

Views: 2468

Answers (1)

Nick
Nick

Reputation: 6965

You can just add a method to your payment model called isAvailable(Mage_Sales_Model_Quote $quote) that returns a bool. For example, in your situation you could add something like:

public function isAvailable($quote = null) {
    $isLoggedIn = Mage::helper('customer')->isLoggedIn();
    return parent::isAvailable($quote) && $isLoggedIn;
}

The Mage_Payment_Model_Method_Free payment method that ships with Magento is an example of a payment method that employs this -- it'll only show if the basket total is zero.

Upvotes: 6

Related Questions