0

So, for various reasons I really kind of need to put a bunch of different payment method objects into a collection, not an array, a collection.

Unfortunately it seems there's no such thing as a Mage_Payment_Method_Collection class to rely on.

More specifically I'm trying to put the output of Mage_Payment_Config::getActiveMethods() into a collection rather than the array that it returns because, reasons.

Now, I know of a few incredibly clunky ways I could still get the data I need into a collection of my own construction but it would also involve creating my own model class and "converting" the payment method objects into that type (carrying over the bare minimum of data required to actually do what I want to do) but that whole setup would be painfully contrived and from my point of view it just seems odd that there's no way to just say "hey Magento, give me a generic collection object that is able to hold a bunch of random objects" so I can feed this into another part of Magento that absolutely insists on taking a collection as input (more specifically a method in one of the Adminhtml block classes).

1

The various loops below show you how to construct your own collection of payment methods.

If you want a collection of all methods, see below:

$collection = new Varien_Data_Collection();
foreach(Mage::getModel('payment/config')->getAllMethods() as $code => $method) {
    $method->setIdFieldName('id');
    $collection->addItem($method);
}

If you want a collection of only active methods, see below:

$collection = new Varien_Data_Collection();
foreach(Mage::getModel('payment/config')->getActiveMethods() as $code => $method) {
    $method->setIdFieldName('id');
    $collection->addItem($method);
}

If you want to get a collection of active payment methods for a given store, see below:

$collection = new Varien_Data_Collection();
foreach(Mage::getModel('payment/config')->getActiveMethods($storeId) as $code => $method) {
    $method->setIdFieldName('id');
    $collection->addItem($method);
}

If this solves the problem, please mark my answer as accepted. Thanks!

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.