#StackBounty: #magento2.3 #sales-order #payment #magento2.3.5 #order-collection How to add payment filter in Order collection factory m…

Bounty: 50

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


Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.