Magento 1.9 – Get Orders by Payment Method

magento-1.9orderspayment-methods

I am developing a cron supposed to capture all the recent orders from a specific payment method, but I currently have a problem getting all the orders from a specific payment method.

I thought that the payment method would be in the sales_flat_order table, but it isn't, so I can not use something like :

$ordersCollection = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('payment_method', $paymentMethod);

How can I filter all the orders and only get the one with a certain payment method ?

Best Answer

Try to do this using joining the collections:

$table_prefix = Mage::getConfig()->getTablePrefix();
$order_table = $table_prefix.'sales_flat_order';
$on_condition = "main_table.parent_id = $order_table.entity_id";

$orderCollection =  Mage::getModel('sales/order_payment')->getCollection()->addFieldToFilter('method',"PAYMENT_METHOD_NAME");    // for e.g checkmo

$orderCollection ->getSelect()->join($order_table,$on_condition);

foreach($orderCollection as $order):
 echo '<br/>ORDER # : '.$order->getIncrementId();
endforeach;

Hope this will help!