Resolving Payment Due Issues When Creating Invoices in Magento 2
The Challenge: Missing Shipping & Handling Amount in Programmatic Invoicing
When creating invoices programmatically in Magento 2 using the Purchase Order payment method, you might face a common issue - the generated invoice excludes the shipping and handling amounts. This results in an incorrect total due amount that needs to be resolved.
Understanding the Current Implementation
Let’s first understand what your current code is doing:
if ($order->getTotalDue() > 0)
{
// ... existing code ...
$invoice = $invoiceService->prepareInvoice($order);
if (!$invoice->getTotalQty())
{
throw new MagentoFrameworkExceptionLocalizedException(__('Cannot create an invoice without products.'));
}
if ($order->getTotalDue() > 0) $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::CAPTURE_ONLINE);
else $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::NOT_CAPTURE);
// ... existing code ...
}
The problem lies in this line: $invoice = $invoiceService->prepareInvoice($order);. This prepares an invoice based on the order’s items, but it doesn’t include any non-item costs like shipping and handling.
The Solution: Manually Adding Shipping & Handling to the Invoice
To resolve this issue, you need to manually add the shipping and handling amounts to the invoice. Here’s how you can modify your code:
if ($order->getTotalDue() > 0)
{
// ... existing code ...
$invoice = $invoiceService->prepareInvoice($order);
if (!$invoice->getTotalQty())
{
throw new MagentoFrameworkExceptionLocalizedException(__('Cannot create an invoice without products.'));
}
// Add shipping and handling manually
foreach ($order->getAllItems() as $item) {
$invoice->addRow(
$item,
$item->getQtyToInvoice()
);
}
if ($shippingAmount = $order->getShippingAmount()) {
$invoice->addRow(
new \Magento\Framework\DataObject([
'name' => 'Shipping & Handling',
'price_incl_tax' => $shippingAmount,
'tax_amount' => 0
])
);
}
if ($order->getTotalDue() > 0) $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::CAPTURE_ONLINE);
else $invoice->setRequestedCaptureCase(MagentoSalesModelOrderInvoice::NOT_CAPTURE);
// ... existing code ...
}
In this updated version, we first prepare the invoice as before. Then, we iterate through all the order items and add them to the invoice using $invoice->addRow(). After that, we check if there is a shipping amount ($order->getShippingAmount()). If there is, we create a new row for “Shipping & Handling” with the correct price (including tax) and add it to the invoice.
Conclusion: Ensuring Complete Invoices
By manually adding the shipping and handling amounts to the invoice as shown above, you can ensure that your programmatic invoices are complete. This will correctly reflect the total due amount in Magento 2 when creating invoices for orders with a Purchase Order payment method.
Remember, it’s always crucial to test changes like these thoroughly before deploying them to a live environment. If you have any further questions or need more complex solutions, feel free to check out our detailed guides on Magento development at Laravel Company.