0

I want to display the list of invoices of logged in customer in the frontend.

1

You can also get the invoice for specific customer in the following

use Magento\Customer\Model\Session; 
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface; 
use Magento\Framework\App\ObjectManager;


/**
     * @var \Magento\Sales\Model\ResourceModel\Order\Collection
     */
    protected $orders;

    /**
     * @var CollectionFactoryInterface
     */
    private $orderCollectionFactory;
    /**
     * @var \Magento\Sales\Model\Order\Config
     */
    protected $_orderConfig;
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    public function __construct(
        Session $customerSession,
        \Magento\Sales\Model\ResourceModel\Order\Collection $orderCollectionFactory,
        \Magento\Sales\Model\Order\Config $orderConfig,
    ) {
        $this->_customerSession = $customerSession;
        $this->_orderCollectionFactory = $orderCollectionFactory;
        $this->_orderConfig = $orderConfig;
    }

Use this code in the function you want to display invoice

$orders =$this->getOrders();
        foreach ($orders as $order) {
            $invoices = $order->getInvoiceCollection();
            foreach ($invoices as $invoice) {
                var_dump($invoice->getData());
            }
        }

These are the function which has been used in the above code

/**
     * @return CollectionFactoryInterface
     *
     * @deprecated 100.1.1
     */
    private function getOrderCollectionFactory()
    {
        if ($this->orderCollectionFactory === null) {
            $this->orderCollectionFactory = ObjectManager::getInstance()->get(CollectionFactoryInterface::class);
        }
        return $this->orderCollectionFactory;
    }

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

Hope this will help you. Thanks

1
0

Try following way:


use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory as InvoiceCollectionFactory;

Now di inject:


public function __construct(
    InvoiceCollectionFactory $invoiceCollectionFactory
) {
    $this->invoiceCollectionFactory = $invoiceCollectionFactory;
}

How you use:


/** @var \Magento\Sales\Model\ResourceModel\Order\Invoice\Collection $invoiceCollection */
$invoiceCollection = $this->invoiceCollectionFactory->create();
$invoiceCollection->getSelect()
    ->join(
        'sales_order',
        'sales_order.entity_id=main_table.order_id'
    )
    ->where('customer_id=?', 1);
3
  • @Rana i will try and let u know. – gajjala sandeep Jan 14 '19 at 16:02
  • @Rana How to pass customer id dynamically based on logged in customer ? – gajjala sandeep Jan 14 '19 at 16:03
  • You can get it from session. Magento\Customer\Model\Session – Sohel Rana Jan 14 '19 at 16:36

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.