Magento – get order collection by payment
If you are working on a Magento module to compute order data, you may have the task to get order collection by payment. This is sometimes needed to separate orders done by pre payment from bank payment orders.
Magento – get order collection by payment
The following litte code snippet gives you all you need to know:
1 2 3 4 5 6 7 | $orderCollection = Mage::getModel('sales/order_payment')->getCollection()->addFieldToFilter('method',"YOUR_PAYMENT_METHOD"); //for example: checkmo $orderCollection ->getSelect()->join("sales_flat_order", "main_table.parent_id = sales_flat_order.entity_id"); foreach($orderCollection as $order){ ... //TODO ... } |
The first line creates a new order payment collection (sales/order_payment). The important part is to add a filter with your desired payment method. For this, check your sales_flat_order_table for payment method names:
Replace now YOUR_PAYMENT_METHOD from my previous code sample with one of these methods. In the second line, we join our sales_flat_order table with condition, that we only need the data for previous filtered orders. This results in a collection with all order data you need for further processing.
Conclusion
With a simple filter and a join you can get only orders that are payed with a given payment method. My sample code helps you to get a ready to use collection, you can use this for loop to export this data in your needed format.
2 Responses
[…] Magento – get order collection by payment by MageMaster […]
[…] Magento – get order collection by payment by MageMaster […]