Orders API – How to Add a Payment Method Filter Field to Order Collection

apifilterorderspayment

At the moment I have this code:

$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('state',array(array('like'=>'processing')));

Is there a way to add a filter to getCollection() to also filter the orders by payment method or description? something along the lines of:

$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('state',array(array('like'=>'processing')))
    ->addFieldToFilter('method',array(array('nlike'=>'paypal')));

If it is only possible by using the filter on single orders, how would i go about doing that? This doesnt seem to work:

$invoices = Mage::getModel('sales/order')->loadByIncrementId($parentOrderNr)
    ->addFieldToFilter('method',array(array('nlike'=>'paypal')));

Best Answer

You need to append the sales_flat_order_payment table to collection:

$collection = Mage::getModel('sales/order')->getCollection()
    ->join(
        array('payment' => 'sales/order_payment'),
        'main_table.entity_id=payment.parent_id',
        array('payment_method' => 'payment.method')
    );

$collection->addFieldToFilter('payment.method', array(array('nlike' => 'paypal')))
    ->addFieldToFilter('state', array(array('like' => 'processing')));
Related Topic