You can get full details of Invoice related data using invoice id in Magento 2.
Using Magento\Sales\Api\InvoiceRepositoryInterface class you can get details of invoice related data in Magento 2.
Get Invoice related data using below code snippet in Magento 2 by Invoice id,
<?php namespace Path\To\Class; use Exception; use Psr\Log\LoggerInterface; use Magento\Sales\Api\Data\InvoiceInterface; use Magento\Sales\Api\InvoiceRepositoryInterface; class InvoiceData { /** * @var InvoiceRepositoryInterface */ private $invoiceRepository; /** * @var LoggerInterface */ private $logger; public function __construct( InvoiceRepositoryInterface $invoiceRepository, LoggerInterface $logger ) { $this->invoiceRepository = $invoiceRepository; $this->logger = $logger; } /** * Get Invoice data * * @return InvoiceInterface|null */ public function getInvoiceData(): ?InvoiceInterface { $invoiceId = 1; try { $invoiceData = $this->invoiceRepository->get($invoiceId); } catch (Exception $exception) { $this->logger->critical($exception->getMessage()); $invoiceData = null; } return $invoiceData; } }
Pass Invoice id to fetch invoice records from a database table.
echo $invoice = $this->getInvoiceData();
Output:
Return Invoice object
Array ( [entity_id] => 1 [store_id] => 2 [base_grand_total] => 815.9800 [shipping_tax_amount] => 0.0000 [tax_amount] => 0.0000 [base_tax_amount] => 0.0000 [store_to_order_rate] => 0.0000 [base_shipping_tax_amount] => 0.0000 [base_discount_amount] => 0.0000 [base_to_order_rate] => 1.0000 [grand_total] => 815.9800 [shipping_amount] => 15.9900 [subtotal_incl_tax] => 799.9900 [base_subtotal_incl_tax] => 799.9900 [store_to_base_rate] => 0.0000 [base_shipping_amount] => 15.9900 [total_qty] => 1.0000 [base_to_global_rate] => 1.0000 [subtotal] => 799.9900 [base_subtotal] => 799.9900 [discount_amount] => 0.0000 [billing_address_id] => 4 [order_id] => 1 [send_email] => 1 [can_void_flag] => 0 [state] => 2 [shipping_address_id] => 3 [store_currency_code] => USD [order_currency_code] => USD [base_currency_code] => USD [global_currency_code] => USD [increment_id] => 2000000001 [created_at] => 2019-07-26 11:38:58 [updated_at] => 2019-07-26 11:38:59 [discount_tax_compensation_amount] => 0.0000 [base_discount_tax_compensation_amount] => 0.0000 [shipping_discount_tax_compensation_amount] => 0.0000 [base_shipping_discount_tax_compensation_amnt] => 0.0000 [shipping_incl_tax] => 15.9900 [base_shipping_incl_tax] => 15.9900 )
Hi, How to get the invoiced product details based on the invoice id on magento 2 ?
@aasikellahi:disqus I have to write separate blog for that. Thanks.
@aasikellahi:disqus I have to write separate blog for that. Thanks.
I had found that. We can get by below comment.
public function getinvoiceordproduct($invoice_entity_id){
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$invoice = $objectManager->create('MagentoSalesApiDataInvoiceInterface')->load($invoice_entity_id);
//Loop through each item and fetch data
foreach ($invoice->getAllItems() as $item)
{
$ordproduct = mb_strimwidth($item['name'], 0, 15, "..." ). "x".floor($item['qty']);
$ordproducts[] = $ordproduct;
}
return $ordproducts;
}