Magento – join tables for get payment method of order id

join-tablemagento-1.9payment-methodssales-order

I need to join 3 tables to get payment method of specific order id
Let me explain my query.

I have to load a custom_id and take saved order id from my table 'custom_data'. Now I want to find which payment method is used for this order id.

Best Answer

// id you are getting from your custom table on which you have to load order_id.

$id = '1';

//Note we are considering order_id of custom table as increment id

try {
    $collection = Mage::getModel('customdata/custom')->getCollection()->addFieldToFilter('main_table.id', $id);
    $collection->getSelect()->join(array('sales_order' => sales_flat_order), 'sales_order.increment_id = main_table.order_id',array());
    $collection->getSelect()->join(array('payment' => sales_flat_order_payment), 'payment.parent_id = sales_order.entity_id', array('payment_method' => 'payment.method'));
    foreach ($collection->getData() as $data) {
        print_r($data);
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

result: Array ( [id] => 1 [order_id] => 100000001 [payment_method] => checkmo ).

Related Topic