Capture partial amount once order is placed

Capture partial amount once order is placed

September 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Capture partial amount once order is placed

Capturing Partial Amounts for Individual Products in Magento

Handling partial payments for individual products within an order is a common requirement for many e-commerce stores. However, Magento’s built-in invoice functionality tends to add all items to the invoice when using the $order->prepareInvoice($qtys) method, even if you’re only trying to capture payment for one specific product.

Let me explain why this issue occurs and provide a step-by-step solution to capture partial amounts for individual products in Magento.

The Problem: Magento Adds All Items to the Invoice

The core problem lies in how Magento processes invoices. When you call $order->prepareInvoice($qtys), Magento assumes that all specified quantities should be included on the invoice, regardless of whether they belong to a single order or multiple orders.

In your provided code snippet, even though you’re only passing an array with a single item (Array([370] => 1)), Magento will include this quantity for every item in the order when preparing the invoice. This is why your partial payment captures all items, not just the specified one.

The Solution: Manually Prepare and Save the Invoice

To capture partial amounts for individual products, you need to manually prepare and save the invoice without using $order->prepareInvoice($qtys). Instead, you can follow these steps:

  1. Load the order.
  2. Initialize an empty array to store the items for the invoice.
  3. Loop through each item in the order and add only the desired quantity to the invoice array.
  4. Use the Mage_Sales_Model_Order::prepareInvoice($items) method with the manually prepared items.
  5. Register, pay, and save the invoice separately.

Here’s an updated version of your code that implements this solution:

$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);

$invoiceItems = array();
foreach ($order->getAllVisibleItems() as $item) {
    if (isset($specificItemQuantities[$item->getId()])) { // Assume $specificItemQuantities is an associative array with item IDs as keys and desired quantities as values
        $invoiceItems[] = array(
            'qty' => $specificItemQuantities[$item->getId()],
            'total_qty' => $item->getQtyOrdered(),
            'row_total' => $item->getRowTotal(),
            'tax_amount' => $item->getTaxAmount(),
        );
    }
}

$invoice = $order->prepareInvoice($invoiceItems);

if (!$invoice->hasInvoices()) {
    Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}

$invoice->register()->pay();
$invoice->save();

$order->addStatusHistoryComment('Partial amount of $'.$invoice->getGrandTotal().' captured automatically.', false);
$order->save();

Key Points to Consider

  1. Make sure the $specificItemQuantities array contains the item IDs from your order as keys and the desired quantities as values.
  2. The code assumes that you have access to the relevant order ID or increment ID when calling loadByIncrementId($order_id).
  3. This approach allows you to capture partial amounts for individual products within a single order.

Conclusion

Capturing partial payments for specific items in Magento requires a manual preparation of invoices without relying on the $order->prepareInvoice($qtys) method. By following the provided solution, you can accurately process partial payments for individual products within an order.

Remember to adapt the code according to your specific requirements and implementation details. If you need further assistance or want to learn more about Magento development, feel free to explore our comprehensive guides at Laravel Company.

Catalog Themes Orders Errors

Our network