0

I need to get all orders on the sales order history page without any limit I need to get the orders. In Magento 2.3.

I had tried the below code using the OrderCollectionFactory but I am getting the error

if (!$this->orders) {

            $this->orders = $this->orderCollectionFactory->create($customerIds)->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'
            );
            
        }

Uncaught Error: Call to a member function create() on null in /var/www/html/app/code/Sm/SalesOrderGridTable/Block/Order/History.php:139 Stack trace: #0

4
  • where backend or frontend ? and please describe more. – Msquare Dec 3 '20 at 12:37
  • At Frontend Bro Sales order History page. – Sabareesh Dec 3 '20 at 13:34
  • @Msquare Did you got any solution for my question. – Sabareesh Dec 4 '20 at 6:48
  • Please check and update me. – Msquare Dec 4 '20 at 9:42
1

app/code/VendoerName/ModuleName/etc

di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
   <preference for="Magento\Sales\Block\Order\History" type="VendoerName\ModuleName\Block\Order\History" />
</config>

app/code/VendoerName/ModuleName/Block/Order

History.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace VendoerName\ModuleName\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 = 'VendoerName_ModuleName::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;
    }

    public function getAllOrder()
    {
        return $this->orderCollectionFactory->create();
    }

    /**
     * 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()]
            )->setOrder(
                'created_at',
                'desc'
            );
        }
        return $this->orders;
    }

    /**
     * @inheritDoc
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if ($this->getOrders()) {
            $pager = $this->getLayout()->createBlock(
                \Magento\Theme\Block\Html\Pager::class,
                'sales.order.history.pager'
            )->setCollection(
                $this->getOrders()
            );
            $this->setChild('pager', $pager);
            $this->getOrders()->load();
        }
        return $this;
    }

    /**
     * Get Pager child block output
     *
     * @return string
     */
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    /**
     * Get order view URL
     *
     * @param object $order
     * @return string
     */
    public function getViewUrl($order)
    {
        return $this->getUrl('sales/order/view', ['order_id' => $order->getId()]);
    }

    /**
     * Get order track URL
     *
     * @param object $order
     * @return string
     * @deprecated 102.0.3 Action does not exist
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function getTrackUrl($order)
    {
        //phpcs:ignore Magento2.Functions.DiscouragedFunction
        trigger_error('Method is deprecated', E_USER_DEPRECATED);
        return '';
    }

    /**
     * Get reorder URL
     *
     * @param object $order
     * @return string
     */
    public function getReorderUrl($order)
    {
        return $this->getUrl('sales/order/reorder', ['order_id' => $order->getId()]);
    }

    /**
     * Get customer account URL
     *
     * @return string
     */
    public function getBackUrl()
    {
        return $this->getUrl('customer/account/');
    }

    /**
     * Get message for no orders.
     *
     * @return \Magento\Framework\Phrase
     * @since 102.1.0
     */
    public function getEmptyOrdersMessage()
    {
        return __('You have placed no orders.');
    }
}

app/code/Dolphin/AutoRelatedProducts/view/frontend/templates/order

history.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// phpcs:disable Magento2.Templates.ThisInTemplate
// @codingStandardsIgnoreFile

/** @var \Magento\Sales\Block\Order\History $block */
?>
<?php $_orders = $block->getAllOrder(); ?>
<?= $block->getChildHtml('info') ?>
<?php if ($_orders && count($_orders)) : ?>
    <div class="table-wrapper orders-history">
        <table class="data table table-order-items history" id="my-orders-table">
            <caption class="table-caption"><?= $block->escapeHtml(__('Orders')) ?></caption>
            <thead>
                <tr>
                    <th scope="col" class="col id"><?= $block->escapeHtml(__('Order #')) ?></th>
                    <th scope="col" class="col date"><?= $block->escapeHtml(__('Date')) ?></th>
                    <?= $block->getChildHtml('extra.column.header') ?>
                    <th scope="col" class="col total"><?= $block->escapeHtml(__('Order Total')) ?></th>
                    <th scope="col" class="col status"><?= $block->escapeHtml(__('Status')) ?></th>
                    <th scope="col" class="col actions"><?= $block->escapeHtml(__('Action')) ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($_orders as $_order) : ?>
                    <tr>
                        <td data-th="<?= $block->escapeHtml(__('Order #')) ?>" class="col id"><?= $block->escapeHtml($_order->getRealOrderId()) ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Date')) ?>" class="col date"><?= /* @noEscape */ $block->formatDate($_order->getCreatedAt()) ?></td>
                        <?php $extra = $block->getChildBlock('extra.container'); ?>
                        <?php if ($extra) : ?>
                            <?php $extra->setOrder($_order); ?>
                            <?= $extra->getChildHtml() ?>
                        <?php endif; ?>
                        <td data-th="<?= $block->escapeHtml(__('Order Total')) ?>" class="col total"><?= /* @noEscape */ $_order->formatPrice($_order->getGrandTotal()) ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Status')) ?>" class="col status"><?= $block->escapeHtml($_order->getStatusLabel()) ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Actions')) ?>" class="col actions">
                            <a href="<?= $block->escapeUrl($block->getViewUrl($_order)) ?>" class="action view">
                                <span><?= $block->escapeHtml(__('View Order')) ?></span>
                            </a>
                            <?php if ($this->helper(\Magento\Sales\Helper\Reorder::class)->canReorder($_order->getEntityId())) : ?>
                                <a href="#" data-post='<?= /* @noEscape */
                                $this->helper(\Magento\Framework\Data\Helper\PostHelper::class)
                                    ->getPostData($block->getReorderUrl($_order))
                                ?>' class="action order">
                                    <span><?= $block->escapeHtml(__('Reorder')) ?></span>
                                </a>
                            <?php endif ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php else : ?>
    <div class="message info empty"><span><?= $block->escapeHtml($block->getEmptyOrdersMessage()) ?></span></div>
<?php endif ?>

Update History.php getAllOrder() function

public function getAllOrder()
{
    $cutId = $this->_customerSession->getCustomerId();
    $orderColl = [];
    if ($cutId) {
        $orderColl = $this->orderCollectionFactory->create();
        $orderColl->addAttributeToFilter('customer_id',$cutId);
    }
    return $orderColl;
}

I Hope This Helps You.

10
  • you can also filter data like this community.magento.com/t5/Magento-2-x-Programming/… – Msquare Dec 4 '20 at 9:44
  • yes i can get the point from your answer by i need to get all order order of the logged in or customer id's which i need also with the filtes. – Sabareesh Dec 7 '20 at 5:36
  • You need to get current customer id and use addAttributeToFilter('customer_id',123456) to collection. – Msquare Dec 7 '20 at 5:53
  • get current customer id try this magento.stackexchange.com/a/171791/82670 – Msquare Dec 7 '20 at 5:55
  • if (!$this->orders) { $this->orders = $this->orderCollectionFactory->create($customerIds)->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' ); } I had tried this please check wheather it will work Getting this error – Sabareesh Dec 7 '20 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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