Magento 2: Filter order collection by payment method
-
14-04-2021 - |
Pregunta
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?
Solución 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();
Otros consejos
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
}
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange