0

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;
        }

2 Answers 2

0
+50

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!

0

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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.