Question

I need to filter order collection based on payment method. I tried the following:

$customerOrderCollection = $this->orderCollectionFactory->create()->addFieldToFilter('customer_id', $customerId)
                                                            ->addFieldToFilter('status', 'pending');
    /* join with payment table */
$customerOrders = $customerOrderCollection->getSelect()
                                              ->join(["sop" => "sales_order_payment"],
                                                      'main_table.entity_id = sop.parent_id',
                                                       array('method')
                                                )
                                              ->where('sop.method = ?',$paymentMethod );

But I am not able to apply getData() method on the above collection. I need to check if at least a single order exists based on above conditions

Is there a way to join collection and also get data?

Was it helpful?

Solution 2

I was able to solve this using the following:

$customerOrderCollection = $this->orderCollectionFactory->create()->addFieldToFilter('customer_id', $customerId)
                                                                ->addFieldToFilter('status', 'pending');
/* join with payment table */
$joinQuery = $customerOrderCollection->getSelect()
                                             ->join(["sop" => "sales_order_payment"],
                                                          'main_table.entity_id = sop.parent_id',
                                                           array('method')
                                                   )
                                              ->where('sop.method = ?',$paymentMethod );
$customerOrders = $customerOrderCollection->load($joinQuery)->getData();

OTHER TIPS

You can simply use foreach and if conditions to get the order filter by Payment method.

$customerOrderCollection = $this->orderCollectionFactory->create()->addFieldToFilter('customer_id', 
                           $customerId)->addFieldToFilter('status','pending');
foreach($customerOrderCollection as $customerOrder) {
   if($customerOrder->getPayment()->getMethod() == $paymentMethod) {
    print_r($customerOrder->getData()); //To display data
    //Your actions
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top