1

I have an invoice id or an invoice increment id, is there a way to load an invoice using these?

3 Answers 3

5

Try this

private $invoice;

public function __construct(
    ....
    \Magento\Sales\Model\Order\Invoice $invoice
) {
    ....
    $this->invoice = $invoice;
}

....
....

// load invoice using entity id
$invoice = $this->invoice->load('1');
// OR
// load invoice using increment id
$invoice = $this->invoice->loadByIncrementId('000000001');

print_r($invoice->getData());
0

Try this :

public function __construct(
    .....
    \Magento\Sales\Model\Order\Invoice $invoice,
    .......
) {
    ...
    $this->invoice = $invoice;
    ........
    parent::__construct($context, $data);
}


public function getInvoiceDetails($incrementId){

    $invoiceDetails = $this->invoice->loadByIncrementId($incrementId);
    echo "<pre>"; print_r($invoiceDetails->getId()); exit;
 }
0

I think it is better to use the invoice interface:

    use Magento\Sales\Api\InvoiceRepositoryInterface;

    private $invoiceRepository;

    public function __construct(
     ...
        InvoiceRepositoryInterface $invoiceRepository
     ...
    ){
        $this->invoiceRepository = $invoiceRepository;
    }
     
    public function myCustomFunction($invoiceEntityId)
    {
        $invoice = $this->invoiceRepository->get($invoiceEntityId);
    }

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.