How to add payment filter in Order collection factory magento 2.3
-
14-04-2021 - |
Question
I need to add the "po_number" as filter in order collection factory, i am trying to implement this in the sales order history page using getOrders function in the History.php
Full Code :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sm\SalesOrderGridTable\Block\Order;
use \Magento\Framework\App\ObjectManager;
use \Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
/**
* Sales order history block
*
* @api
* @since 100.0.2
*/
class History extends \Magento\Framework\View\Element\Template
{
/**
* @var string
*/
protected $_template = 'Magento_Sales::order/history.phtml';
/**
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
*/
protected $_orderCollectionFactory;
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
/**
* @var \Magento\Sales\Model\Order\Config
*/
protected $_orderConfig;
/**
* @var \Magento\Sales\Model\ResourceModel\Order\Collection
*/
protected $orders;
/**
* @var CollectionFactoryInterface
*/
private $orderCollectionFactory;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Sales\Model\Order\Config $orderConfig
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
\Magento\Customer\Model\Session $customerSession,
\Magento\Sales\Model\Order\Config $orderConfig,
array $data = []
) {
$this->_orderCollectionFactory = $orderCollectionFactory;
$this->_customerSession = $customerSession;
$this->_orderConfig = $orderConfig;
parent::__construct($context, $data);
}
/**
* @inheritDoc
*/
protected function _construct()
{
parent::_construct();
$this->pageConfig->getTitle()->set(__('My Orders'));
}
/**
* Provide order collection factory
*
* @return CollectionFactoryInterface
* @deprecated 100.1.1
*/
private function getOrderCollectionFactory()
{
if ($this->orderCollectionFactory === null) {
$this->orderCollectionFactory = ObjectManager::getInstance()->get(CollectionFactoryInterface::class);
}
return $this->orderCollectionFactory;
}
/**
* Get customer orders
*
* @return bool|\Magento\Sales\Model\ResourceModel\Order\Collection
*/
public function getOrders()
{
if (!($customerId = $this->_customerSession->getCustomerId())) {
return false;
}
if (!$this->orders) {
$this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
'*'
)->addFieldToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)->addFieldToFilter(
'entity_id',
['num' => 362]
)->setOrder(
'ext_order_id',
'desc'
);
}
$this->orders->getSelect()
->join(
["sop" => $this->getTable("sales_order_payment")],
'main_table.entity_id = sop.parent_id'
)
->where('sop.po_number = ?','08RRR');
echo "<pre>"; print_r($this->orders->getData()); die("dead");
return $this->orders;
}
}
Specific get Orders():
public function getOrders()
{
if (!($customerId = $this->_customerSession->getCustomerId())) {
return false;
}
if (!$this->orders) {
$this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
'*'
)->addFieldToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)->addFieldToFilter(
'entity_id',
['num' => 362]
)->setOrder(
'ext_order_id',
'desc'
);
}
$this->orders->getSelect()
->join(
["sop" => $this->getTable("sales_order_payment")],
'main_table.entity_id = sop.parent_id'
)
->where('sop.po_number = ?','08RRR');
echo "<pre>"; print_r($this->orders->getData()); die("dead");
return $this->orders;
}
Solution 2
I got My answer by by using the below code
public function getOrders()
{
//print_r($this->getRequest()->getParam('po_num'));
$po_number = "%".$this->getRequest()->getParam('po_num')."%";
if (!($customerId = $this->_customerSession->getCustomerId())) {
return false;
}
if (!$this->orders) {
$this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
'*'
)->addFieldToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
);
$this->orders->getSelect()
->joinLeft(
["sop" => "amt_sales_order_payment"],
'main_table.entity_id = sop.parent_id',
['po_number']
)
->where('sop.po_number LIKE ?',$po_number);
$this->orders->setOrder(
'ext_order_id',
'desc'
);
//echo $this->orders->getSelect()->__toString();
}
return $this->orders;
}
OTHER TIPS
Try the below code in your function getOrders()
to add payment filter
$this->sales_order_table = "main_table";
$this->sales_order_payment_table = $this->getTable("sales_order_payment");
$this->orders->getSelect()
->join(array('sop' =>$this->sales_order_payment_table), $this->sales_order_table . '.entity_id= sop.parent_id');
$this->orders->getSelect()->where("po_number=".$po_number);
Or try below way to join the table:
/* join payment table */
$this->orders->getSelect()
->join(
["sop" => $this->getTable("sales_order_payment")],
'main_table.entity_id = sop.parent_id',
array('po_number')
)
->where('sop.po_number = ?','08RRR');
Hope this helps!
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange