I have an invoice id or an invoice increment id, is there a way to load an invoice using these?
3 Answers
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());
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;
}
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);
}