Magento 2 Order Collection – How to Retrieve

collection;magento2orders

how to get order collection in custom module ? like My Orders collection in customers dashboard (frontend). this collection how to get?

Best Answer

Refer the following function:

Class : Magento\Sales\Block\Order\History

 /**
     * @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;
    }

If have any issues. Please comment. I will try to resolve.