Magento Asked on December 14, 2021
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 SmSalesOrderGridTableBlockOrder;
use MagentoFrameworkAppObjectManager;
use MagentoSalesModelResourceModelOrderCollectionFactoryInterface;
/**
* Sales order history block
*
* @api
* @since 100.0.2
*/
class History extends MagentoFrameworkViewElementTemplate
{
/**
* @var string
*/
protected $_template = 'Magento_Sales::order/history.phtml';
/**
* @var MagentoSalesModelResourceModelOrderCollectionFactory
*/
protected $_orderCollectionFactory;
/**
* @var MagentoCustomerModelSession
*/
protected $_customerSession;
/**
* @var MagentoSalesModelOrderConfig
*/
protected $_orderConfig;
/**
* @var MagentoSalesModelResourceModelOrderCollection
*/
protected $orders;
/**
* @var CollectionFactoryInterface
*/
private $orderCollectionFactory;
/**
* @param MagentoFrameworkViewElementTemplateContext $context
* @param MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory
* @param MagentoCustomerModelSession $customerSession
* @param MagentoSalesModelOrderConfig $orderConfig
* @param array $data
*/
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory,
MagentoCustomerModelSession $customerSession,
MagentoSalesModelOrderConfig $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|MagentoSalesModelResourceModelOrderCollection
*/
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;
}
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;
}
Answered by Sabareesh on December 14, 2021
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!
Answered by Bhaumik Upadhyay on December 14, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP