0

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.

4
  • What Magento Version? Do you need a raw mysql query or a magento model?
    – Pawel
    Sep 23 '16 at 11:31
  • Magento 1.9.x . I want to use magento model
    – roine
    Sep 23 '16 at 11:32
  • Whats the exact structure of your custom tables?
    – Pawel
    Sep 23 '16 at 11:57
  • here is my fields : id,customer_name,grand_total,order_id ..... I will load Id in this table and get order_id.
    – roine
    Sep 23 '16 at 12:56
2

// 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 ).

0

And In Magento 2 I would do:

/** @var $orders \Magento\Sales\Model\ResourceModel\Order\Collection */
$orders = $this->orderCollectionFactory->create();
$orders->addFieldToFilter('store_id', $storeId);
$orders->getSelect()->joinLeft(
    ['payment' => 'sales_order_payment'],
    'payment.parent_id = main_table.entity_id',
    ['payment_method' => 'payment.method']
);

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.